Mongoose Middleware pre 'remove' 不起作用,Model.update 不是函数
Mongoose Middleware pre 'remove' doesn't work, Model.update is not a function
我为 Book
模式设置了一个中间件查询:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var User= require('./user');
var schema = new Schema({
name : {type: String, required:true},
startDate: {type: Date},// UTC datetime
endDate: {type: Date},// UTC datetime
status: {type: String},
user: {type: Schema.Types.ObjectId, ref: 'User'}
});
schema.post('remove', function(next) {
User.update(
{ books: this._id},
{ $pull: { books: this._id } })
.exec();
next();
});
module.exports = mongoose.model('Book', schema);
如您所见,它试图从用户的图书列表中删除该图书。作为参考,请参阅 User
架构(顺便说一下,此架构中的 post 查询有效):
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Book = require('./book');
var schema = new Schema({
firstName: {type: String, required: true},
lastName: {type: String},
phone1: {type: String},
phone2: {type: String},
email: {type: String},
address: {type: Schema.Types.ObjectId, ref: 'Address'},
books: [{type: Schema.Types.ObjectId, ref:'Book'}]
});
schema.post('remove', function (user) {
Book.findById({$in : user.books}, function (err, book) {
if (err) {
return res.status(500).json({
title: 'An error occurred cascade delete operations for user',
error: err
});
}
if (book) {
book.user= undefined;
book.save();
}
});
});
module.exports = mongoose.model('User', schema);
我不断收到此错误,我尝试了多种查询变体但无济于事:
process.nextTick(function() { throw err; });
TypeError: User.update is not a function
有人可以帮帮我吗?
这可能是由于使用 require().
将每个模型导入另一个模型导致的循环依赖
尝试使用 require() 来导入您的模型:
var User = require('./user')
...和:
var Book = require('./book')
相反,在您需要使用模型时从 mongoose 获取模型,例如:
mongoose.model('User').update(...)
...和:
mongoose.model('Book').findById(...)
另请注意,您可能需要在计划使用它们的文件中使用 require() 导入这两个模块,以便在使用前向 mongoose 注册这两个模型.
希望对您有所帮助。
我认为你应该改用 .pre() 。希望这样的东西对你有用:
bookSchema.pre('remove', function (next) {
var book = this;
book.model('User').update(
{ books: book._id },
{ $pull: { books: book._id } },
{ multi: true },
next);
});
我为 Book
模式设置了一个中间件查询:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var User= require('./user');
var schema = new Schema({
name : {type: String, required:true},
startDate: {type: Date},// UTC datetime
endDate: {type: Date},// UTC datetime
status: {type: String},
user: {type: Schema.Types.ObjectId, ref: 'User'}
});
schema.post('remove', function(next) {
User.update(
{ books: this._id},
{ $pull: { books: this._id } })
.exec();
next();
});
module.exports = mongoose.model('Book', schema);
如您所见,它试图从用户的图书列表中删除该图书。作为参考,请参阅 User
架构(顺便说一下,此架构中的 post 查询有效):
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Book = require('./book');
var schema = new Schema({
firstName: {type: String, required: true},
lastName: {type: String},
phone1: {type: String},
phone2: {type: String},
email: {type: String},
address: {type: Schema.Types.ObjectId, ref: 'Address'},
books: [{type: Schema.Types.ObjectId, ref:'Book'}]
});
schema.post('remove', function (user) {
Book.findById({$in : user.books}, function (err, book) {
if (err) {
return res.status(500).json({
title: 'An error occurred cascade delete operations for user',
error: err
});
}
if (book) {
book.user= undefined;
book.save();
}
});
});
module.exports = mongoose.model('User', schema);
我不断收到此错误,我尝试了多种查询变体但无济于事:
process.nextTick(function() { throw err; });
TypeError: User.update is not a function
有人可以帮帮我吗?
这可能是由于使用 require().
将每个模型导入另一个模型导致的循环依赖尝试使用 require() 来导入您的模型:
var User = require('./user')
...和:
var Book = require('./book')
相反,在您需要使用模型时从 mongoose 获取模型,例如:
mongoose.model('User').update(...)
...和:
mongoose.model('Book').findById(...)
另请注意,您可能需要在计划使用它们的文件中使用 require() 导入这两个模块,以便在使用前向 mongoose 注册这两个模型.
希望对您有所帮助。
我认为你应该改用 .pre() 。希望这样的东西对你有用:
bookSchema.pre('remove', function (next) {
var book = this;
book.model('User').update(
{ books: book._id },
{ $pull: { books: book._id } },
{ multi: true },
next);
});