所以我四处寻找试图弄清楚这一点,我想我只是错过了一些简单的东西......但我无法完全弄清楚。
使用 Mongoose,我正在查找特定文档,当找到该文档时,我想查看字段 senderDel 是否设置为 true,然后我将进行删除。如果它设置为 false,我会进行更新。
代码:
PMessage.delete = function(msgId, callback)
{
Message.find({_id: msgId}, function(e, res){
if (res){
console.log(res);
if (res.senderDel == true){
console.log("sender deleted...call message remove");
Message.remove({_id: msgId}, callback);
} else {
console.log("res.senderDel is not true");
Message.findByIdAndUpdate(msgId, {$set: {receiverDel: true}}, callback);
}
} else {
console.log("error: " + e);
callback;
}
});
};
console.log(res) 返回:
[ { sender: 50ea4e8198c6770e1e000004,
senderName: 'tester',
receiver: 50ac4ac5bca9f1794b000007,
receiverName: 'admin',
subject: 'a',
body: 'a',
_id: 5101a35a7889727e35000006,
__v: 0,
receiverDel: false,
senderDel: true,
read: false,
sentOn: '01/24/2013 21:10:50' } ]
我会认为我可以用 res.senderDel 引用 senderDel 字段?为什么 res.senderDel 出现未定义,我该如何修改它以使其工作?任何帮助是极大的赞赏!
编辑:添加我的架构:
var messageSchema = new Schema({
sender: {type: ObjectId, ref: 'User'},
senderName: String,
receiver: {type: ObjectId, ref: 'User'},
receiverName: String,
subject: String,
body: String,
sentOn: {type: String, default: moment().format()},
read: {type: Boolean, default: false},
senderDel: {type: Boolean, default: false},
receiverDel: {type: Boolean, default: false}
});
module.exports = mongoose.model('Message', messageSchema);