我定义了一个猫鼬用户模式:
var userSchema = mongoose.Schema({
email: { type: String, required: true, unique: true},
password: { type: String, required: true},
name: {
first: { type: String, required: true, trim: true},
last: { type: String, required: true, trim: true}
},
phone: Number,
lists: [listSchema],
friends: [mongoose.Types.ObjectId],
accessToken: { type: String } // Used for Remember Me
});
var listSchema = new mongoose.Schema({
name: String,
description: String,
contents: [contentSchema],
created: {type: Date, default:Date.now}
});
var contentSchema = new mongoose.Schema({
name: String,
quantity: String,
complete: Boolean
});
exports.User = mongoose.model('User', userSchema);
friends参数定义为对象ID的数组。因此,换句话说,一个用户将拥有一个包含其他用户ID的数组。我不确定这是否是执行此操作的正确符号。
我正在尝试将新的Friend推送到当前用户的friend数组:
user = req.user;
console.log("adding friend to db");
models.User.findOne({'email': req.params.email}, '_id', function(err, newFriend){
models.User.findOne({'_id': user._id}, function(err, user){
if (err) { return next(err); }
user.friends.push(newFriend);
});
});
但这给了我以下错误:
TypeError:对象531975a04179b4200064daf0没有方法'cast'
[{"$oid": "dfdfsdfsg565ls"},...]
而不是["hkugh87tgkgk", ....]