I am new to React JS and I created a simple application which accept UserName and Email and inserts to MongoDB.
I use React + Node + Express + Mongoose + MongoDB and I was able to insert the record successfully
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
})
Now, I want to return 'numRowsAffected' from DB.js to App.js.
Hence, I modified DB.js by adding callback to mongoose save() function
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 });
}
});
});
However, I don't know how to change the code on App.js (ie in axios.post) to fetch the return value from DB.js
Any help is highly appreciated
You can use
Mongoose.update()withupsertoption set to true instead ofMongoose.save()and read thenModifiedandnInsertedproperty ofupdatereturn value.You can see this post for more detail.