0
我需要從後中間獲取文檔ID,但是當findAndUpdate時它不存在。使用前/保存中間件Mongoose模型方法時獲取文檔ID
任何人都知道爲什麼?
const mongoose = require('mongoose'),
Schema = mongoose.Schema,
ShiftSchema = new Schema({
_id: { type: String, 'default': shortid.generate},
creator_id: { type: String, required: true },
project_id: { type: String, required: true }
}),
_ = require('underscore');
var when_to_notify = ['save', 'findByIdAndUpdate', 'findOneAndUpdate', 'findByIdAndUpdate'];
_.each(when_to_notify, function(pre_func) {
ShiftSchema.post(pre_func, function() {
otherFunc(this);
});
})
function otherFunc(self) {
doSomethingWith(self._id)
// typeof self._id == 'undefined' == true
}