Javascript 未调用Pre函数(使用它自动递增变量,但它不起作用)
这是我的函数前声明Javascript 未调用Pre函数(使用它自动递增变量,但它不起作用),javascript,node.js,mongodb,mongoose,mongoose-sequence,Javascript,Node.js,Mongodb,Mongoose,Mongoose Sequence,这是我的函数前声明 locationSchema.pre('save', function(next) { console.log(" HELLO ") //not printing var doc = this; Counter.findByIdAndUpdate({_id: 'location_id'}, {$inc: {seq: 1} }, {upsert: true , new: true}, function(error, counter) {
locationSchema.pre('save', function(next) {
console.log(" HELLO ") //not printing
var doc = this;
Counter.findByIdAndUpdate({_id: 'location_id'},
{$inc: {seq: 1} },
{upsert: true , new: true}, function(error, counter) {
if(error)
return next(error);
doc.locationId = counter.seq;
next();
});
});
这是我的locationSchema声明,我正在尝试自动递增locationId
const locationSchema = mongoose.Schema({
locationId:{
type:Number,
unique:true
}
});
const mongoose = require('mongoose')
这是负责递增字段的计数器模式
const counterSchema = mongoose.Schema({
_id: {type: String, required: true},
seq: {type: Number, default: 0}
});
const counterModel = mongoose.model('counter', counterSchema)
module.exports={counterSchema,counterModel}
这可能会有帮助:这可能会有帮助: