我正在研究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"
的帖子没有。有帮助吗?
作为参考,这是节点中的post route:
router.post("/", async (req, res) => {
try {
const newCollection = await Collection.create(req.body);
res.send(newCollection);
} catch (error) {
res.send(error.message);
}
});
感谢Pedro Brost,我想通了。在“网络”下,在Dev Tools中,由于我的mongoose模式设置方式,我收到了验证错误。我不知道“网络”是开发工具中的一件事。