我是后期网络开发的新手,目前正在学习数据关联 ...我收到的错误对我来说似乎很严重,因为我是这个主题的新手。 ..我试图在 userSchema 的 postArray 中添加发布的 ObjectId 。< / p>
当我向特定用户添加第一篇帖子时,我没有达到我的预期......我希望将 ObjectId 推入 postArray 但实际发生的事情是整个 newPost 被推入 postArray 。这是遇到的第一个问题。我在这里没有错误,但我没有得到我的预期!
第二个是当我向 postArray 添加另一个帖子时,它给我一个巨大的错误命名 ValidationError ..我花了好几个小时试图修复此错误,这可能是是一个小的,但对我来说是一种痛苦。我的代码如下所示,错误也发布在下面!
var mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/testdatabase");
var postSchema = new mongoose.Schema({
title: String,
content: String
});
var Post = mongoose.model("Post", postSchema);
var userSchema = new mongoose.Schema({
name: String,
email: String,
postArray: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Post'
}]
});
var User = mongoose.model("User", userSchema);
var alex = new User({
name: "Alexander",
email: "alexander@gmail.com"
});
alex.save();
Post.create({
title: "I'm terribly stuck here!",
content: "Does anyone have an idea of where I'm stuck?"
}, function(err, newPost) {
User.findOne({name: "Alexander"}, function(err, foundUser) {
if(err) {
console.log(err);
} else {
console.log("THE NEWLY ADDED POST IS" + newPost);
foundUser.postArray.push(newPost);
foundUser.save(function(err, savedData) {
if(err) {
console.log(err);
} else {
console.log(savedData);
}
});
}
});
});
Blackdeath:~/environment/Associations $ node references.js THE NEWLY ADDED POST IS{ _id: 5a5845c180a490137330bcae, title: 'I\'m terribly stuck here!', content: 'Does anyone have an idea of where I\'m stuck?', __v: 0 } { ValidationError: User validation failed: postArray: Cast to [undefined] failed for value "[{"_id":"5a5844112d4c6512beb16846","title":"This makes me sick!","content":"I\'m stuck here..Someone save me!","__v":0}]" at path "postArray" at MongooseError.ValidationError (/home/ec2-user/environment/Associations/node_modules/mongoose/lib/error/validation.js:27:11) at model.Document.invalidate (/home/ec2-user/environment/Associations/node_modules/mongoose/lib/document.js:1643:32) at _init (/home/ec2-user/environment/Associations/node_modules/mongoose/lib/document.js:413:18) at init (/home/ec2-user/environment/Associations/node_modules/mongoose/lib/document.js:381:5) at model.Document.$__init (/home/ec2-user/environment/Associations/node_modules/mongoose/lib/document.js:351:3) at model.syncWrapper [as $__init] (/home/ec2-user/environment/Associations/node_modules/kareem/index.js:234:23) at model.Document.init (/home/ec2-user/environment/Associations/node_modules/mongoose/lib/document.js:319:8) at completeOne (/home/ec2-user/environment/Associations/node_modules/mongoose/lib/query.js:1980:12) at Immediate.<anonymous> (/home/ec2-user/environment/Associations/node_modules/mongoose/lib/query.js:1502:11) at Immediate.<anonymous> (/home/ec2-user/environment/Associations/node_modules/mquery/lib/utils.js:119:16) at runCallback (timers.js:672:20) at tryOnImmediate (timers.js:645:5) at processImmediate [as _immediateCallback] (timers.js:617:5) errors: { postArray: { ValidatorError: Cast to [undefined] failed for value "[{"_id":"5a5844112d4c6512beb16846","title":"This makes me sick!","content":"I\'m stuck here..Someone save me!","__v":0}]" at path "postArray" at MongooseError.ValidatorError (/home/ec2-user/environment/Associations/node_modules/mongoose/lib/error/validator.js:25:11) at _init (/home/ec2-user/environment/Associations/node_modules/mongoose/lib/document.js:413:37) at init (/home/ec2-user/environment/Associations/node_modules/mongoose/lib/document.js:381:5) at model.Document.$__init (/home/ec2-user/environment/Associations/node_modules/mongoose/lib/document.js:351:3) at model.syncWrapper [as $__init] (/home/ec2-user/environment/Associations/node_modules/kareem/index.js:234:23) at model.Document.init (/home/ec2-user/environment/Associations/node_modules/mongoose/lib/document.js:319:8) at completeOne (/home/ec2-user/environment/Associations/node_modules/mongoose/lib/query.js:1980:12) at Immediate.<anonymous> (/home/ec2-user/environment/Associations/node_modules/mongoose/lib/query.js:1502:11) at Immediate.<anonymous> (/home/ec2-user/environment/Associations/node_modules/mquery/lib/utils.js:119:16) at runCallback (timers.js:672:20) at tryOnImmediate (timers.js:645:5) at processImmediate [as _immediateCallback] (timers.js:617:5) message: 'Cast to [undefined] failed for value "[{"_id":"5a5844112d4c6512beb16846","title":"This makes me sick!","content":"I\\\'m stuck here..Someone save me!","__v":0}]" at path "postArray"', name: 'ValidatorError', properties: [Object], kind: 'cast', path: 'postArray', value: '[{"_id":"5a5844112d4c6512beb16846","title":"This makes me sick!","content":"I\'m stuck here..Someone save me!","__v":0}]', reason: undefined } }, _message: 'User validation failed', name: 'ValidationError' }
答案 0 :(得分:0)
发现错误..我无法将mongoose包添加到package.json
起初,我刚刚做了
npm install mongoose
后来我尝试了
npm install mongoose --save
它有效!谢谢!