当我向服务器发出请求时,数据将作为承诺(如预期的那样)返回,其中包含正确的数据,但是由于某些原因,程序无法正确执行。
此外,在我将其上载到Zeit之前,该程序是可行的。
获取请求:
1)我知道我不需要'content-type':'application / json'。删除它不会影响程序。
2)这是一个GET请求,端点正常工作。另外,当我在Postman中执行相同的确切请求时,也会得到正确的结果。
// CODE ABOVE DOES NOT MATTER
.then( ([hours,employees,dayLabor]) => {
// hours,and dayLabor are empty lists for this exercise '[]'
let business = fetch(`${config.URL}/${TokenService.getId()}`,{
headers: {
'content-type': 'application/json','table':'business','Authorization':`bearer ${TokenService.getauthToken()}`
}
})
.then(data => {
console.log('business: ',data,' or ',data.json());
if (!data.ok){
return data.json().then(e => Promise.reject(e));}
return data.json();
});
return Promise.all([business,hours,dayLabor]);
})
.then( ([business,dayLabor]) => {
// THIS is never executed?
console.log('completed');
//fetch has been completed and the state has been updated so set "fetched" to true
this.setState({business,'dayLabor': dayLabor.length>0? this.sort(dayLabor):[],fetched: true});
})
.catch(error => {
console.error({error});
});
输出结果(出于隐私原因将网址括起来):
business:
Response {type: "cors",url: "https://xxxxxx.herokuapp.com/1",redirected: false,status: 200,ok: true,…}
type: "cors"
url: "https://xxxxxxxx.herokuapp.com/1"
redirected: false
status: 200
ok: true
statusText: "OK"
headers: Headers {}
body: (...)
bodyUsed: true
__proto__: Response
or
Promise {<pending>}
__proto__: Promise
[[PromiseStatus]]: "resolved"
[[PromiseValue]]: Array(1)
0: {id: 1,business_name: "Fake Company Inc"}
length: 1
__proto__: Array(0)
我想访问“ [[PromiseValue]]”中的对象。我对为什么它不起作用感到困惑,尤其是因为当我在本地运行它时它确实起作用了?看来问题出在“业务”变量或“ Promise.all”不在等待承诺解决。
任何帮助将不胜感激,我一直在搜索,找不到任何解决方案。