【问题标题】:Solution to trigger the middleware when updating of a subdocument更新子文档时触发中间件的解决方案
【发布时间】:2021-03-04 11:07:05
【问题描述】:

我使用这种地址架构并在多个架构中使用它。 一切正常,只有当我更新地址时

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 的文档仅涉及中间件保存和验证。

https://mongoosejs.com/docs/subdocs.html#what-is-a-subdocument-

但是做我想做的最好的解决方案是什么?


其他信息

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"
    },

当我们创建新客户时,addressSchema 的中间件“save”没有问题,但是当我们更新客户时,所有数据都正确更新,但不会触发 addressSchema 的中间件“updateOne”。

【问题讨论】:

  • 您能否发布未在中间件中触发的 pointSchema 和更新查询。
  • 此地址架构是客户架构的子架构吗?如果是,请在您的问题中说明。如果您也发布更新查询也会有所帮助。
  • 它是多个模式的子模式,它用于每个具有地址的模式

标签: mongoose updates middleware


【解决方案1】:

我不确定是否有任何直接的方法可以做到这一点,但您可以为您的所有父架构 customerSchemaproviderSchema 创建预中间件,并设置地址是否更新等条件,

  • 创建一个函数来处理地址架构上的操作(如果它正在更新)
function preHookForAddress (_this) {
   // if its "address" object
   if (_this.get('address')) {
        let address = _this.get('address');
        // operation stuff here
        // ..
        // set after update
        _this.set({ address: address });
   }

   // if its "address.location" object
   if (_this.get('address.location')) {
        let address_location = _this.get('address.location');
        // operation stuff here
        // ..
        // set after update
        _this.set({ "address.location": address_location });
   }
}

customerSchema.pre('updateOne', async function (next) {
    await preHookForAddress(this);
    next();
});

providerSchema.pre('updateOne', async function (next) {
    await preHookForAddress(this);
    next();
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-10
    • 2017-10-25
    相关资源
    最近更新 更多