I use this schema of addresses and use it in several schemas. All works fine, only when I update a address
const addressSchema = new mongoose.Schema({
street: String,
number: String,
postcode: String,
city: String,
comment: String,
country: {
type: 'ObjectId',
ref: 'Country'
},
location: {
type: pointSchema,
default: () => ({}),
index: '2dsphere',
}
});
addressSchema.pre('save', async function (next) {
console.log('subdocument address pre.save'); // is triggred
//create geolocation
});
addressSchema.pre('updateOne', async function (next) {
console.log('subdocument address pre.updateOne'); // not triggred, no error
//update geolocation
});
Mongoose's documation speak only about middelware save and validate.
https://mongoosejs.com/docs/subdocs.html#what-is-a-subdocument-
But what is the best solution to do what I want ?
Additional informations
const pointSchema = new mongoose.Schema({
type: {
type: String,
enum: ['Point'],
default: 'Point'
},
coordinates: {
type: [Number],
default: null,
}
});
const common = require('./common');
const customerSchema = new Schema({
//...
address: {
type: common.addressSchema,
default: () => ({}),
},
})
const providerSchema = new Schema({
//...
address: {
type: common.addressSchema,
default: () => ({}),
},
})
"address": {
"_id": "6043a5f3a8cad615cd8eab3a",
"location": {
"type": "Point",
"coordinates": [
48.818511962890625,
2.31961989402771
],
"_id": "603fed0629390b30d53bed3e"
},
"street": "Avenue des Tulipes",
"postcode": "78500",
"number": "69",
"city": "Montrouge",
"comment": null,
"country": "5f626eb337fd4d75ab694112"
},
When we create a new customer we have no problem with addressSchema's middleware 'save', but when we update a Customer, all datas are updated correctly, but the addressSchema's middleware 'updateOne' isn't triggered.