Javascript api 获得调用 returns 响应,但在与 .then 链接时未定义
Javascript api get call returns response but is undefined when chained with .then
我有以下片段。 api 调用成功并且 api returns 预期数据。
但是,当我在 .then(payload)
块内执行控制台日志并记录有效负载时,它是未定义的。这是为什么?
return API.get.MyData()
.then(response => {
console.log('response', response); // this is valid
response.json();
})
.then(payload => {
console.log('payload', payload); // this is undefined... why???
};
您需要 return response.json()
才能在链接中访问它 .then
通常,您会这样做:
fetch(...).then(r => r.json()).then(console.log);
隐式 returns r.json()
,但由于您使用的是 () => { ... }
,您需要显式 return
.
return API.get.MyData()
.then(response => {
console.log('response', response); // this is valid
return response.json();
})
.then(payload => {
console.log('payload', payload); // this is undefined... why???
};
我有以下片段。 api 调用成功并且 api returns 预期数据。
但是,当我在 .then(payload)
块内执行控制台日志并记录有效负载时,它是未定义的。这是为什么?
return API.get.MyData()
.then(response => {
console.log('response', response); // this is valid
response.json();
})
.then(payload => {
console.log('payload', payload); // this is undefined... why???
};
您需要 return response.json()
才能在链接中访问它 .then
通常,您会这样做:
fetch(...).then(r => r.json()).then(console.log);
隐式 returns r.json()
,但由于您使用的是 () => { ... }
,您需要显式 return
.
return API.get.MyData()
.then(response => {
console.log('response', response); // this is valid
return response.json();
})
.then(payload => {
console.log('payload', payload); // this is undefined... why???
};