【发布时间】:2018-09-18 13:58:12
【问题描述】:
我正在开发一个 MERN 应用程序。当我通过 Postman 向"/collections" 发出发布请求时,我得到了预期的响应,并且数据库使用新文档进行了更新。当我在浏览器中发出请求时,代码运行,但数据库没有获得新文档。这是我的提交处理程序(reactjs):
handleSubmit = async e => {
e.preventDefault();
try {
await axios.post("/collections", this.state.data);
} catch (error) {
console.log(error.message);
}
this.props.history.push("/collections");
};
当我提交相关表单时,该代码的最后一行运行,我的浏览器返回到"/collections"。代码不会抛出或记录任何错误,但提交不会创建新文档,正如 mongodb Compass 所揭示的那样。同样奇怪的是,这段代码是从应用程序的另一部分修改的,在浏览器中运行良好:
handleSubmit = async e => {
e.preventDefault();
await axios.post("/entries", this.state.data);
this.props.history.push("/entries");
};
这很好用,但发给"/collections" 的帖子不行。有什么帮助吗?
作为参考,这是节点中的发布路线:
router.post("/", async (req, res) => {
try {
const newCollection = await Collection.create(req.body);
res.send(newCollection);
} catch (error) {
res.send(error.message);
}
});
【问题讨论】:
-
您应该在浏览器中打开开发工具并告诉我们请求发生了什么