I am using MongoJS and would like to auto-create a new collection called "Comments" in my MongoDB via the code below...Not working and can't seem to figure out why. Thanks for any insight you can provide!
app.post("/post", function(req, res) {
db.comments.insert({
articleId: JSON.stringify(req.body._Id),
name: JSON.stringify(req.body.chatId),
message: JSON.stringify(req.body.message)
}),function(err, response) {
if(err) {
console.log(err);
} else {
console.log("This is working!");
}
}});
If you use the body-parse middleware:
app.post("/post", function(req, res) {
let { body } = req;
db.comments.insert({
articleId: body._Id,
name: body.chatId,
message: body.message
}),function(err, response) {
if(err) {
console.log(err);
} else {
console.log("This is working!");
}
}});
and if not , just use it like:
let parsed;
try {
parsed = JSON.parse(res.text)
} catch(err){req.send(err)}
// now you have:
parsed._Id ...