我正在尝试保存与另一个模型有关系的模型:
exports.portfolioSchema = new Schema({
name: {type: String, required: true, unique: true},
isActive: {type: Boolean, default: true}
});
exports.projectSchema = new Schema({
name: {type: String, required: true, unique: true},
isActive: {type: Boolean, required: true, default: true},
portfolio: {type: mongoose.Schema.Types.ObjectId, ref: 'portfolioSchema', required: true},
isCodeBaseFullyOwned: {type: Boolean, required: true, default: false}
});
尝试保存时:
Project = store.model('Project', schema.projectSchema);
var p = new Project();
p.name = project;
Portfolio.findOne({"name":activePortoflios[0]}, '_id', function (err, data) {
p.portfolio = data._id;
});
p.isActive = true;
p.save(function (err) {
if (err) console.log(err);
});
我收到以下错误:
{ ValidationError: Project validation failed
at MongooseError.ValidationError (/Users/sasha/PycharmProjects/P5N/node_modules/mongoose/lib/error/validation.js:23:11)
at model.Document.invalidate (/Users/sasha/PycharmProjects/P5N/node_modules/mongoose/lib/document.js:1524:32)
at /Users/sasha/PycharmProjects/P5N/node_modules/mongoose/lib/document.js:1399:17
at validate (/Users/sasha/PycharmProjects/P5N/node_modules/mongoose/lib/schematype.js:706:7)
at /Users/sasha/PycharmProjects/P5N/node_modules/mongoose/lib/schematype.js:743:9
at Array.forEach (native)
at ObjectId.SchemaType.doValidate (/Users/sasha/PycharmProjects/P5N/node_modules/mongoose/lib/schematype.js:711:19)
at /Users/sasha/PycharmProjects/P5N/node_modules/mongoose/lib/document.js:1397:9
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
errors:
{ portfolio:
{ ValidatorError: Path `portfolio` is required.
at MongooseError.ValidatorError (/Users/sasha/PycharmProjects/P5N/node_modules/mongoose/lib/error/validator.js:24:11)
at validate (/Users/sasha/PycharmProjects/P5N/node_modules/mongoose/lib/schematype.js:705:13)
at /Users/sasha/PycharmProjects/P5N/node_modules/mongoose/lib/schematype.js:743:9
at Array.forEach (native)
at ObjectId.SchemaType.doValidate (/Users/sasha/PycharmProjects/P5N/node_modules/mongoose/lib/schematype.js:711:19)
at /Users/sasha/PycharmProjects/P5N/node_modules/mongoose/lib/document.js:1397:9
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
message: 'Path `portfolio` is required.',
name: 'ValidatorError',
properties: [Object],
kind: 'required',
path: 'portfolio',
value: undefined } },
message: 'Project validation failed',
name: 'ValidationError' }
我有什么遗失的吗? data._id
产生类型ObjectId,我还需要设置其他内容吗?
答案 0 :(得分:0)
findOne
是异步调用。因此,当您尝试保存它时,不会返回findOne
回调。由于属性portfolio
是必需的,因此未通过验证。
Project = store.model('Project', schema.projectSchema);
var p = new Project();
p.name = project;
Portfolio.findOne({ name: activePortoflios[0] }, '_id', function (err, data) {
if (err) return handleError(err);
p.portfolio = data._id;
p.isActive = true;
p.save(function (err) {
if (err) console.log(err);
});
});