This is my pre function declaration
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();
});
});
This is my locationSchema declaration, I'm trying to auto increment locationId
const locationSchema = mongoose.Schema({
locationId:{
type:Number,
unique:true
}
});
const mongoose = require('mongoose')
This is my counter Schema responsible for incrementing the field
const counterSchema = mongoose.Schema({
_id: {type: String, required: true},
seq: {type: Number, default: 0}
});
const counterModel = mongoose.model('counter', counterSchema)
module.exports={counterSchema,counterModel}