Свойство runValidator не работает на upsert на mongodb с mongoose - PullRequest
0 голосов
/ 20 апреля 2020

Моя модель имеет функцию проверки, которая проверяет ForeignKey.

Ниже приведен код моей модели для свойства locationId.

locationId: {
            type: mongoose.SchemaTypes.ObjectId,
            required: true,
            validate: {
                isAsync: true,
                validator: function(v) {
                    return FKHelper(mongoose.model('Account_LocationMapping'), v);
                },
                message: `LocationId doesn't exist`
            }
          }

Помощник FK

module.exports = (model, id) => {
    return new Promise((resolve, reject) => {
        model.findOne({ _id: id }, (err, result) => {
            if (result) {
                return resolve(true);
            } else
                return reject(
                    new Error(`FK Constraint 'checkObjectsExists' for '${id.toString()}' failed`)
                );
        });
    });
};

Мой запрос upsert:

 updateOne: {
               filter: {
                         accountId: req.account
                       },
               update: {
                         $set: { locationId: req.location },
                         //runValidator:true -> tried setting the validator property inside update.
                       },
               upsert: true,
               runValidators: true
            }

Оба способа настройки runValidator: true не работают.

...