0

I am getting this error in the pre findOneAndupdate Hook up

uncaughtException: Cannot read property 'update' of undefined

Middleware hook up code:

purchaseSchema.pre('findOneAndUpdate', function (next) {

  this.model.findOne(this._conditions)
    .then(purchaseOrder => {
      if (!this._update.is_draft && (!this._update.code && !purchaseOrder.code)) {
        codeSequence('PurchaseSchema', 'purchaseOrders', 'FC', function (err, sequence) {
          if (err) {
            new Error("Error al generar secuencia: ", err.message);
          } else {
            this.update({}, {
              $set: {
                code: sequence
              }
            })
            next();
          }
        });
      } else {
        next();
      }
    })
    .catch(err => next(err));
});

I am using mongoose 4.13.5 I don't understand why I am getting this error now

joselegit
  • 533
  • 1
  • 14
  • 35
  • What exactly are you trying to do here? In your `else` part try the following code instead: `purchaseOrder.code = sequence; purchaseOrder.save()` – dnickless Nov 27 '17 at 20:09
  • inside the else I have other validations. The error occur in the sentence `this.update({}, { $set: { code: sequence } })` – joselegit Nov 28 '17 at 13:51
  • I think you've got multiple issues here. But one looks like it's this one here: https://stackoverflow.com/questions/4886632/what-does-var-that-this-mean-in-javascript – dnickless Nov 28 '17 at 15:09

0 Answers0