- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试创建一个博客并返回以下模式中填充的博客:
const blogSchema = new mongoose.Schema({
title: {
type:String
},
author: {
type: mongoose.Schema.Types.ObjectID,
ref: 'UserTable',
required: true
}
});
module.exports = mongoose.model('BlogPostTable', blogSchema);
和
const userSchema = new mongoose.Schema({
username:{
type:String,
},
blogPosts: [
{
type: mongoose.Schema.Types.ObjectID,
ref: 'BlogPostTable'
}
]
});
module.exports = mongoose.model('UserTable', userSchema);
我正在保存这样的博客:
blogRouter.post('/', async (request, response, next) => {
const token = request.token;
try {
const foundUser = await userTable.findById(decodedToken.id); // Find User
const newBlog = new blogTable({ // Create document
title: request.body.title,
text: request.body.text,
likes: 0,
author: foundUser._id
});
await newBlog.save(); // Save Blog
foundUser.blogPosts = foundUser.blogPosts.concat(newBlog); // update Users blogs
await foundUser.save();
response.status(200).json(newBlog.populate('author').toJSON()); // WRONG OUTPUT
}
但是,作者填写错误。没有用户名
,id
是一个数组!
我哪里出了问题以及如何解决?
最佳答案
您可以添加以下代码行来查看代码中发生的情况:
mongoose.set('调试', true);
第一条语句:await newBlog.save();
触发 insertOne
操作,文档的 author
设置为: author: ObjectId("...")
然后运行 awaitfoundUser.save();
显式设置博客文章数组:
{ '$set': { blogPosts: [ ObjectId(...), ObjectId(...) ] }
这是有道理的,因为您在 JS 代码中使用了 concat
。问题是,没有其他第三个查询,因为您尝试在现有内存对象上运行 populate
,但这是行不通的 - populate 需要查询而不是内存对象。
因此,您必须再次查询数据库才能填充author
:
let userPosts = await blogTable
.find({ author: foundUser._id })
.populate('author');
console.log(userPosts);
触发两个查询:
Mongoose: blogposttables.find({ author: ObjectId("...") }, { projection: {} })
Mongoose: usertables.find({ _id: { '$in': [ ObjectId("...") ] } }, { projection: {} })
关于node.js - Mongoose 中 document.save() 后的人口数量错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60461320/
我的网页上有两个 DIV 元素。如果 DIV A 的内部 HTML 是通过不受我控制的第三方脚本添加的,我想删除 DIV B 的内部 html。 在 JS 中实现这一目标的最佳方法是什么? 最佳答案
我整个周末都在看这段代码。我知道有数据,我可以通过其他查询提取它,但 GridView 不会显示和填充。有什么建议吗? string sqlSelection =
所以表格在页面上 然后我使用 AJAX 返回数据 $.ajax({ type: 'POST', url: "ticketAjax.php", data: '&m=swapTi
我有一个表格 View Controller ,我正在使用 Storyboard设计它。原型(prototype)单元非常基本,它只有两个标签。姓名标签和日期标签。 当我运行程序时,样式就在那里(字体
我正在尝试填充不可变 JS 映射。给定一个字段数组和一个表单,例如 {form: 'Register', fields: ['name','firstname']} 我想得到类似的东西: { form
我有两个 Mongoose 模型: 1-事件模型: eventSchema = new mongoose.Schema({ name: { type: String, requir
我想以编程方式填充 WPF ListView 。我认为我接近答案但尚未解决,我使用一种方法来填充 ListView , ListView 在 XAML 中看起来像这样
我有以下场景: 用户可以登录网站。用户可以添加/删除投票(有两个选项的问题)。任何用户都可以通过选择任何选项来对民意调查发表意见。 考虑到上述情况,我有三个模型 - Users Polls Optio
这个问题在这里已经有了答案: Capture __LINE__ and __FILE__ without #define (2 个答案) 关闭 3 年前。 所以我有一个要评估结果的函数,如果结果失败
我有一个使用 innerHTML 在表中添加行的函数: function addRowToTable(tblParam) { var tbl =document.getElementById(tb
我是一名优秀的程序员,十分优秀!