架构选项_id:false和文档在保存错误发生之前必须具有_id
我正在尝试通过这种方式创建一个用户文档:
// create the document ---------------
const id = mongoose.Types.ObjectId()
let userDoc = await Admin.create({ ...req.body, _id: id, by: id })
架构:
adminSchema = new mongoose.Schema({
// some other fields, firstName, lastName ... etc
by: {
type: mongoose.Schema.ObjectId,
ref: 'Admin',
required: [true, "the 'by' field is required"],
immutable: true,
}
}, { _id: false })
模型:
const Admin = mongoose.model('Admin', adminSchema, 'users')
- 我的架构没有
_id
属性。
现在,我想拥有_id
字段,并且字段具有相同的值,该值是服务器端生成的ID。
Mongoose正在抛出此错误:
错误:MongooseError:文档在保存之前必须具有_id .../node_modules/mongoose/lib/model.js:291:18
更新:
我更新了我的问题,我添加了架构选项,现在我知道发生了此错误的原因。这是因为我设置的_id:false
架构选项。但是我需要此选项,因为我不想在发送给客户的响应中看到_id
s。有解决方法吗?因为此选项看起来像是在做两个无关的事情
I am trying to create a user document through this way:
// create the document ---------------
const id = mongoose.Types.ObjectId()
let userDoc = await Admin.create({ ...req.body, _id: id, by: id })
Schema:
adminSchema = new mongoose.Schema({
// some other fields, firstName, lastName ... etc
by: {
type: mongoose.Schema.ObjectId,
ref: 'Admin',
required: [true, "the 'by' field is required"],
immutable: true,
}
}, { _id: false })
Model:
const Admin = mongoose.model('Admin', adminSchema, 'users')
- My schema doesn't have an
_id
property.
Now I want to have the _id
field and the by
field has the same value, which is a server-side generated id.
Mongoose is throwing this error:
Error: MongooseError: document must have an _id before saving at
.../node_modules/mongoose/lib/model.js:291:18
update:
I updated my question, I added the schema options, and now I know the reason why this error is happening. it's because of the _id: false
schema option that I have set. But I need this option because I don't want to see _id
s in the responses that I send to the clients. is there a workaround? because this option looks like its doing two unrelated things
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用Mongoose 6.4
我通过删除
_id:false
架构类型选项来解决此问题。并从响应中删除_id,而不必用_。motit() s或 delete> delete 到处我在模式中添加了以下模式类型选项:
现在真正的问题是,为什么在服务器端生成ID时,简单地添加选项
_id:false
会导致猫鼬错误没有猫鼬的帮助?我部分回答了我自己的问题,但是为此,我真的不知道。
Using Mongoose 6.4
I solved this by removing the
_id: false
schema type option.and to remove the _id from the responses without having to pollute the routes with _.omit()s or deletes everywhere, I added the following schema type options to the schema:
Now the real question is, why does simply adding the option
_id: false
results in the Mongoose error when you're generating the id on the server-side without the help of Mongoose?I partially answered my own question, but for this one... I really don't know.
根据您的评论,如果您希望用户收到的响应不包含
_id
您可以:_id
属性,并在没有_id (或创建一个新对象以避免问题)。
一个简短的例子可能是:
Based on your comment, if you want the response the user receives to not contain the
_id
you can:_id
property and return this object without the_id
(Or create a new object to avoid problems).A brief example could be: