我有这种更新文档的方法
static updateData(data: any) {
return new Promise((resolve, reject) => {
nudgesDB.find({ _id: data.id }, (err, nudge: any) => {
if (err) {
reject(err);
}
nudge = nudge[0];
if (data.isRead) {
nudge.isRead = data.isRead;
}
if (data.isDelivered) {
nudge.isDelivered = data.isDelivered;
}
// if (data.deliveryResult) {
// if (nudge.deliveryResult && nudge.deliveryResult.length > 0) {
// nudge.deliveryResult.push(data.deliveryResult[0]);
// } else {
// nudge.deliveryResult = data.deliveryResult;
// }
// }
nudge.save( (error: any, updatedNudge: any) => {
if (error) return reject(error);
resolve(updatedNudge);
});
});
});
}
我正在尝试传递一个新的deliveryResult结构,该结构是一个数组,但是由于某种原因,deliveryResult第一次仍然具有旧的架构 即使在我注释掉代码后,它仍然可以保存deliveeryResult
这是我的收藏的模式:
const nudgesSchema = new mongoose.Schema({
sourceId: String,
registererGUID: String,
createdTime: Date,
originationDateTime: Date,
expiryDateTime: Date,
source: Object,
title: String,
target: String,
sendTo: String,
isRead: Boolean,
isPush: Boolean,
isDelivered: Boolean,
bodyType: String,
body: {
message: String,
},
priority: Number,
intent: Object,
deliveryResult: Object
});
知道为什么会这样吗?
答案 0 :(得分:0)
尝试添加.lean(),以便它将从mongo返回纯对象结果,然后您可以编辑其结果。
static updateData(data: any) {
return new Promise((resolve, reject) => {
nudgesDB.find({ _id: data.id }).lean().exec((err, nudge: any) => {
if (err) {
reject(err);
}
nudge = nudge[0];
if (data.isRead) {
nudge.isRead = data.isRead;
}
if (data.isDelivered) {
nudge.isDelivered = data.isDelivered;
}
// if (data.deliveryResult) {
// if (nudge.deliveryResult && nudge.deliveryResult.length > 0) {
// nudge.deliveryResult.push(data.deliveryResult[0]);
// } else {
// nudge.deliveryResult = data.deliveryResult;
// }
// }
nudgesDB.update({sourceId: nudge.sourceId}, {$set: nudge}).exec((err, updatedNudge) => {
if (error) return reject(err);
resolve(updatedNudge);
})
});
});
}