【发布时间】:2021-03-08 19:06:42
【问题描述】:
我正在使用 Mongoose 和 Javascript (NodeJS) 来读取/写入 MongoDB。我有一个文档(Parent),里面有一堆子文档(Children)。我的文档和子文档都在其模型中定义了验证(required: true 和一个验证用户是否将文本放入字段中的函数)。
当尝试将新的子文档推送到数据库时,Mongoose 拒绝了我的推送,因为对文档的验证失败。这让我感到困惑,因为我没有尝试使用子文档创建新文档,我只是尝试将新的子文档推送到现有文档中。
这是我的(示例)猫鼬模型:
const mongoose = require('mongoose');
const requiredStringValidator = [
(val) => {
const testVal = val.trim();
return testVal.length > 0;
},
// Custom error text
'Please supply a value for {PATH}',
];
const childrenSchema = new mongoose.Schema({
childId: {
type: mongoose.Schema.Types.ObjectId,
},
firstName: {
type: String,
required: true,
validate: requiredStringValidator,
},
lastName: {
type: String,
required: true,
validate: requiredStringValidator,
},
birthday: {
type: Date,
required: true,
},
});
const parentSchema = new mongoose.Schema(
{
parentId: {
type: mongoose.Schema.Types.ObjectId,
},
firstName: {
type: String,
required: true,
validate: requiredStringValidator,
},
lastName: {
type: String,
required: true,
validate: requiredStringValidator,
},
children: [childrenSchema],
},
{ collection: 'parentsjustdontunderstand' },
);
const mongooseModels = {
Parent: mongoose.model('Parent', parentSchema),
Children: mongoose.model('Children', childrenSchema),
};
module.exports = mongooseModels;
我可以通过以下 MongoDB 命令成功地将新的 Child 子文档推送到 Parent 文档中:
db.parentsjustdontunderstand.update({
firstName: 'Willard'
}, {
$push: {
children: {
"firstName": "Will",
"lastName": "Smith",
"birthday": "9/25/1968" }
}
});
但是,当我按照 Mongoose 文档 Adding Subdocs to Arrays 并尝试通过 Mongoose 添加它时,它失败了。
出于测试目的,我正在使用 Postman 并对端点执行 PUT 请求。
以下为req.body:
{
"firstName": "Will",
"lastName": "Smith",
"birthday": "9/25/1968"
}
我的代码是:
const { Parent } = require('parentsModel');
const parent = new Parent();
parent.children.push(req.body);
parent.save();
我得到的是:
ValidationError: Parent validation failed: firstName: Path `firstName` is required...`
它列出了所有父文档的验证要求。
我可以在我做错的事情上寻求帮助。作为记录,我在 Stackoverflow 上查看了这个答案:Push items into mongo array via mongoose,但我看到的大多数示例都没有在他们的 Mongoose 模型中展示或讨论验证。
编辑 1
根据@jf 的反馈,我将代码修改为以下内容(将正文移出req.body 并在代码中创建它以用于测试目的。当我尝试以推荐的方式推送更新时,记录被插入,但是,我仍然收到向控制台抛出的验证错误:
const parent = await Parent.findOne({firstName: 'Willard'});
const child = {
children: {
"firstName": "Will",
"lastName": "Smith",
"birthday": "9/25/1968"
}
}
parent.children.push(child);
parent.save();
ValidationError: Parent validation failed: children.12.firstName: Path `firstName` is required., children.12.lastName: Path `lastName` is required., children.12.birthday: Path `birthday` is required.
【问题讨论】:
-
您正在创建一个空的
Parent并尝试保存到数据库中。创建的父对象不需要任何属性(如firstName),是一个空对象,只有属性children,这就是失败。
标签: javascript node.js mongodb mongoose