【发布时间】:2021-01-21 09:31:26
【问题描述】:
我是 React JS 的新手,我创建了一个简单的应用程序,它接受用户名和电子邮件并插入到 MongoDB。
我使用React + Node + Express + Mongoose + MongoDB 并且能够成功插入记录
DB.js
router.route("/insert").post(function(req, res) {
let comments = new Comments(req.body);
console.log(req.body)
comments.save();
});
App.js
axios.post("http://localhost:4000/insert", {
UserName: username,
UserEmail: email,
Comments: comments
})
现在,我想将 'numRowsAffected' 从 DB.js 返回到 App.js。
因此,我修改了 DB.js,为 mongoose save() 函数添加了回调
router.route("/insert").post(function(req, res) {
let comments = new Comments(req.body);
console.log(req.body)
comments.save(function(err, comments, numRows) {
if ( err ) {
res.send(err);
}
else {
res.json({ message: 'Comments added', data: numRows });
}
});
});
但是,我不知道如何更改 App.js (ie in axios.post) 上的代码以从 DB.js
非常感谢任何帮助
【问题讨论】:
标签: reactjs mongoose axios mern