将对象推送到数组后猫鼬对象未正确保存 属性
mongoose object not saving properly after pushing an object to an array property
我的用户架构包含一个通知 属性,它的值是一个对象数组(每个对象都是一个通知)
var UserSchema = new mongoose.Schema({
//a bunch of other user properties.
notifications: [{
notification: String,
directLink: String,
notificationType: Number, //0=vote, 1=comment, 2= answer, 3=post from watched user, 4=watch
onObject: { //not necessary if type (above) is 4
type: String, //post, comment, answer
id: String //id. but not as mongoose object because it is not necessary in this case.
},
date: {type: Date, default: Date.now, index: true},
newNotification: {type: Boolean, default: true}
}]
});
我的问题是当我尝试添加新通知时。当有人 post 发布了新消息,或者有人回复了你的评论等时,将调用一个名为 newNotification 的函数,并将要通知的用户作为参数传递给此函数,形式为 post.author(post 是一个 mongoose 对象,作者是其属性之一,值为用户的 ObjectId,它在传递到函数之前已填充)。
在新的通知函数中,准备好对象,然后将其推送到 user.notifications 数组:
var newNot = {
notification: notification,
directLink: directLink,
notificationType: action[0],
onObject: on
}
//this code is at the end of the function, newNot now contains the data for the new notification.
//the user is passed in as "notifyUser"
console.log(">>>>>>>>"+notifyUser);
console.log(">>>>>>>>>"+notifyUser.notifications);
notifyUser.notifications.push(newNot);
console.log(">>>>>>>>>>>"+notifyUser.notifications);
console.log(">>>>>>>>>>>>>>>"+notifyUser);
notifyUser.save();
问题是通知没有保存。它工作正常,它作为通知添加给用户。它的默认设置的属性甚至被正确添加。但它不保存
通用方法对另一个用户 属性 非常有效,这只是一个增加的数字,所以这不是问题。但我仍然尝试为用户查询数据库,然后在回调中 运行 这些内容,但更改仍未保存。我花了几个小时试图弄清楚为什么它没有被正确保存,但一无所获。
注意:不,做一个通知模式不会更好,在这种特定情况下,这是不必要的。
我发现了问题,由于我在模式中犯了一个错误,所以没有保存对用户的更改,我心不在焉地将单词 "type" 用作 onObject 对象中的键。这里:
onObject: {
type: String, //post, comment, answer
id: String //id. but not as mongoose object because it is not necessary in this case.
}
这让 mongoose 认为 onObject 是一个字符串。实际上我的意思是让它成为一个对象。将密钥类型重命名为其他名称解决了问题。
onObject: { //not necessary if type (above) is 4
objectType: String, //post, comment, answer
id: String //id. but not as mongoose object because it is not necessary in this case.
},
我之前找不到问题,因为我没有发现错误,我替换了:
notifyUser.save();
无声地失败了:
notifyUser.save(function(err){
if (err){
console.log(err);
}
});
(使用 findByIdAndUpdate 也无声地失败,而使用 update 和 $push 给出了不清楚的错误消息)。
我不会删除这个问题,因为万一有人遇到类似的问题,这可能会有所帮助
我的用户架构包含一个通知 属性,它的值是一个对象数组(每个对象都是一个通知)
var UserSchema = new mongoose.Schema({
//a bunch of other user properties.
notifications: [{
notification: String,
directLink: String,
notificationType: Number, //0=vote, 1=comment, 2= answer, 3=post from watched user, 4=watch
onObject: { //not necessary if type (above) is 4
type: String, //post, comment, answer
id: String //id. but not as mongoose object because it is not necessary in this case.
},
date: {type: Date, default: Date.now, index: true},
newNotification: {type: Boolean, default: true}
}]
});
我的问题是当我尝试添加新通知时。当有人 post 发布了新消息,或者有人回复了你的评论等时,将调用一个名为 newNotification 的函数,并将要通知的用户作为参数传递给此函数,形式为 post.author(post 是一个 mongoose 对象,作者是其属性之一,值为用户的 ObjectId,它在传递到函数之前已填充)。
在新的通知函数中,准备好对象,然后将其推送到 user.notifications 数组:
var newNot = {
notification: notification,
directLink: directLink,
notificationType: action[0],
onObject: on
}
//this code is at the end of the function, newNot now contains the data for the new notification.
//the user is passed in as "notifyUser"
console.log(">>>>>>>>"+notifyUser);
console.log(">>>>>>>>>"+notifyUser.notifications);
notifyUser.notifications.push(newNot);
console.log(">>>>>>>>>>>"+notifyUser.notifications);
console.log(">>>>>>>>>>>>>>>"+notifyUser);
notifyUser.save();
问题是通知没有保存。它工作正常,它作为通知添加给用户。它的默认设置的属性甚至被正确添加。但它不保存 通用方法对另一个用户 属性 非常有效,这只是一个增加的数字,所以这不是问题。但我仍然尝试为用户查询数据库,然后在回调中 运行 这些内容,但更改仍未保存。我花了几个小时试图弄清楚为什么它没有被正确保存,但一无所获。
注意:不,做一个通知模式不会更好,在这种特定情况下,这是不必要的。
我发现了问题,由于我在模式中犯了一个错误,所以没有保存对用户的更改,我心不在焉地将单词 "type" 用作 onObject 对象中的键。这里:
onObject: {
type: String, //post, comment, answer
id: String //id. but not as mongoose object because it is not necessary in this case.
}
这让 mongoose 认为 onObject 是一个字符串。实际上我的意思是让它成为一个对象。将密钥类型重命名为其他名称解决了问题。
onObject: { //not necessary if type (above) is 4
objectType: String, //post, comment, answer
id: String //id. but not as mongoose object because it is not necessary in this case.
},
我之前找不到问题,因为我没有发现错误,我替换了:
notifyUser.save();
无声地失败了:
notifyUser.save(function(err){
if (err){
console.log(err);
}
});
(使用 findByIdAndUpdate 也无声地失败,而使用 update 和 $push 给出了不清楚的错误消息)。
我不会删除这个问题,因为万一有人遇到类似的问题,这可能会有所帮助