编辑
好的,所以我的方法很好。出于某种原因,当我将 const response = await...
更改为 const res = await..
(或除“response”之外的任何其他名称)时,它工作.. 如果这不是内存泄漏,那么我不知道这到底是怎么回事。如果有人有任何见解,我将不胜感激。
我正在向我的客户提出请求:
const config = { headers: { "Content-Type": "application/json" } };
const data = JSON.stringify({ postId });
console.log('sending request'); // prints
const response = await axios.post(
`http://${GATEWAY}:5000/api/posts/single`,
data,
config
);
console.log("response received"); // never reached
但是 request received
从未被打印出来。
我的后端路由有这个,
const post = await Post.findById(postId).populate("likes");
console.log(post); // prints post
return res.json(post);
它会适本地找到帖子并将其记录到控制台。我不确定发生了什么。任何地方都不会打印错误,应用程序会在一段时间后崩溃。它可能正在等待响应。
还有,当我这样做的时候
返回 res.json(null)
我的客户收到响应。但是当我尝试返回 post
或者即使我尝试返回
return res.json( { msg: "Hello World"} );
挂起。
此外,我在整个应用程序中执行类似的 axios 请求——它们按预期工作和运行。不确定我在这里做错了什么。
我试过,
const response = await axios.get(
`http://${GATEWAY}:5000/api/posts/${postId}`,
);
但它的行为和失败方式相同。如果我让请求挂起太久,应用程序就会放弃并崩溃。
另外请注意,我使用的是 axios
而不是 axios-react-native
最佳答案
你确定是“http”而不是“https”
或
否则试试:
const url = `${apiBaseUrl}/someUrlText/someUrlText/`;
const headers = {headers: { "Content-Type": "application/json"}};
const body = JSON.stringify({ postId });
axios
.post(url, body, headers)
.then(res => {
console.log("request received",res);
})
.catch(err => console.log("TODO: Handle error axios", err));
关于node.js - 具有 native react 的 Axios 不返回文档并使应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57770503/