React Native 中 'double fetch' 调用的问题
Problem with a 'double fetch' call in React Native
我在 React Native 函数中使用 'nested' Fetch 调用时遇到问题。似乎第一个 Fetch 工作正常,但第二个会引发错误。这是代码:
//****CALL TWO FETCH REQUESTS...
const data = { passkey: '12345', callup: 'name' };
const secondary = { passkey: '12345', callup: 'name' };
fetch('https://myremoteserveraddress', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then(function(response) {
if (response.ok) {
return response.json();
} else {
return Promise.reject(response);
}
})
.then(data => {
// Store the post data to a variable
_post = data;
console.log('Success on FIRST FETCH:', data);
console.log('answer is:', data.answer);
console.log('answer is:', _post.answer);
// Fetch another API
fetch('https://myremoteserveraddress', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(secondary),
})
})
.then(function (response) {
if (response.ok) {
return response.json();
} else {
return Promise.reject(response);
}
})
.then(function (userData) {
console.log('Returned from BOTH fetch calls'); //does not write to console
console.log(_post, userData); //does not write to console
this.vb.start();
})
.catch((error) => {
console.error('Error in onPressPublishBtn:', error);
});
//****
似乎是第二个 Fetch 调用 returns 'undefined',尽管与第一个 Fetch 调用相同,但似乎运行成功。返回的错误是“TypeError: undefined is not an object (evaluating 'response.ok')”。如果有人可以就问题所在提出建议,我将不胜感激。提前谢谢你。
您应该 return 来自第二个 then(...) 块的 Promise,以便将响应传递给第三个 then( ...) 块。你可能想尝试这样的事情:
// Fetch another API
return fetch('https://myremoteserveraddress', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(secondary),
})
我在 React Native 函数中使用 'nested' Fetch 调用时遇到问题。似乎第一个 Fetch 工作正常,但第二个会引发错误。这是代码:
//****CALL TWO FETCH REQUESTS...
const data = { passkey: '12345', callup: 'name' };
const secondary = { passkey: '12345', callup: 'name' };
fetch('https://myremoteserveraddress', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then(function(response) {
if (response.ok) {
return response.json();
} else {
return Promise.reject(response);
}
})
.then(data => {
// Store the post data to a variable
_post = data;
console.log('Success on FIRST FETCH:', data);
console.log('answer is:', data.answer);
console.log('answer is:', _post.answer);
// Fetch another API
fetch('https://myremoteserveraddress', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(secondary),
})
})
.then(function (response) {
if (response.ok) {
return response.json();
} else {
return Promise.reject(response);
}
})
.then(function (userData) {
console.log('Returned from BOTH fetch calls'); //does not write to console
console.log(_post, userData); //does not write to console
this.vb.start();
})
.catch((error) => {
console.error('Error in onPressPublishBtn:', error);
});
//****
似乎是第二个 Fetch 调用 returns 'undefined',尽管与第一个 Fetch 调用相同,但似乎运行成功。返回的错误是“TypeError: undefined is not an object (evaluating 'response.ok')”。如果有人可以就问题所在提出建议,我将不胜感激。提前谢谢你。
您应该 return 来自第二个 then(...) 块的 Promise,以便将响应传递给第三个 then( ...) 块。你可能想尝试这样的事情:
// Fetch another API
return fetch('https://myremoteserveraddress', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(secondary),
})