我正在使用MongoJS,并想通过下面的代码在MongoDB中自动创建一个名为“ Comments”的新集合...不起作用,似乎无法弄清原因。感谢您提供的任何见解!
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!");
}
}});
答案 0 :(得分:0)
如果您使用正文解析中间件:
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!");
}
}});
,如果不是,则使用它,例如:
let parsed;
try {
parsed = JSON.parse(res.text)
} catch(err){req.send(err)}
// now you have:
parsed._Id ...