繁体   English   中英

带有本机反应的 Axios 不返回文档并使应用程序崩溃

[英]Axios with react native not returning document and crashes app

编辑

好的,所以我的方法很好。 出于某种原因,当我将const response = await...更改为const res = await.. (或除“响应”之外的任何其他名称)时,它起作用了.. 如果这不是内存泄漏,那么我不知道这到底是什么。 如果有人有任何见解,我将不胜感激。


我正在向我的客户提出请求:

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);

它会适当地找到帖子并将其记录到控制台。 我不确定发生了什么。 任何地方都没有打印错误,一段时间后应用程序崩溃。 估计是在等回复。

另外,当我做

return 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));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM