【发布时间】:2021-02-19 04:17:10
【问题描述】:
当通知被删除时,我试图自动删除来自用户的通知引用
userSchema 看起来像
const mongoose = require("mongoose");
const userSchema = new mongoose.Schema({
notifications: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Notification",
},
],
});
module.exports = mongoose.model("User", userSchema);
通知模型如下所示
const mongoose = require("mongoose");
const notificationSchema = new mongoose.Schema({
type: String,
created: {
type: Date,
default: Date.now,
},
});
notificationSchema.pre("deleteOne", function (next) {
this.model("User").deleteOne({ notifications: this._id }, next);
});
module.exports = mongoose.model("Notification", notificationSchema);
当我尝试删除通知时,我收到一个错误,看起来像
ObjectParameterError: Parameter "obj" to Document() must be an object, got User
还有一种方法可以使用 TTL 通知自动删除它[用户中通知的引用]
【问题讨论】:
标签: javascript arrays mongodb mongoose mongoose-schema