我在" catch schema"中添加了一个预删除钩子。删除"位置架构中的所有引用。"
这是我的捕捉模型:
var mongoose = require("mongoose");
var catchSchema = mongoose.Schema({
species: String,
weight: String,
image: String,
catchlocation: String,
catchlocationid: String,
description: String,
timePosted: { type: Date, default: Date.now },
author: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
username: String
}
},
{timestamps: true}
);
catchSchema.pre("remove", function(next) {
console.log("The remove function has fired");
Location.remove({ catches: this._id },
next);
});
module.exports = mongoose.model("Catch", catchSchema);
以下是持有参考文献的位置模式:
var locationSchema = new mongoose.Schema({
name: String,
gps: String,
thumbnail: String,
image: String,
description: String,
catches: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Catch"
}
]
});
var Location = mongoose.model('Location', locationSchema);
这里奇怪的是删除" catch"正在触发pre-remove钩子,但是没有删除引用的ObjectId。