node.js - Mongo在尝试保存数组时返回[object Object]
问题描述
我正在尝试在数组中保存某种评论。该数组有两个值,用户名和评论本身,但是当我尝试保存时,它总是返回 [object Object]。
我尝试的第一种方法是保存评论
// Add the new comment to the blog post's array
lesson.comments.push({
comment: req.body.comment, // Comment field
commentator: user.username // Person who commented
});
如果有问题
// Save blog post
lesson.save((err) => {
// Check if error was found
if (err) {
res.json({ success: false, message: err }); // Return error message
} else {
res.json({ success: true, message: 'Comment saved' }); // Return success message
}
});
但这没有用
然后我尝试改变方式findByIdAndUpdate
Lesson.findByIdAndUpdate(req.body.id,
{
$push: {
"comments.comment":req.body.comment,
"comments.commentator": req.body.id
}
},
{
new:true
},
function(err,course){
if(err){
res.json({success:false, message:err});
console.log(req);
}else{
res.json({success:true, message:req.body.comment});
//res.json({user});
}
}
)
}
但我找不到问题
一切看起来都很好
解决方案
推荐阅读
- kubernetes - Kubernetes:通过 traefik 入口控制器处理集群中多个 LoadBalancer 的连接
- angular - 在 Angular 中迭代 *ngFor 时修复文字中的破折号
- php - PHP 处理超时。如何优化/排队以避免超时
- nginx - 工作进程的 Openresty 动态生成
- java - 在 Java 中读取 Docx 文件页数的最快方法(文字渲染后)?
- php - 通过 URL 传递多个查询参数
- node.js - 这与 npm 无法下载依赖项有关
- hazelcast - 配置 Hazelcast Map 备份时,它是否也映射了 max-size 包括备份条目?
- sql - 从字段中删除不需要的字符
- ios - 应用内购买:设备上的应用商店收据验证:如果用户将设备日期更改为过去,如何处理这种情况