Monday 8 May 2017

NodeJs Mongoose SubDocuments Validation is not working on findOneAndUpdate

This is My Model Schema:

const BalanceSchema = new Schema({
   name: { type: String, required: true },
   rate: { type: Number, required: true },
   amount: { type: Number, required: true },
});

const InvoiceSchema = new Schema({
   key: { type: String, required: true, match: /(EST|INV|BIL)-\w+/ },
   name: { type: String, required: true },
   taxes: [BalanceSchema]
});

And This How I Update my documents:

Invoice.findOneAndUpdate({
   _id: request.params.invoiceId,
  }, { status: request.body.status }, { new: true, runValidators: true }).exec(function (error, invoice) {
    if (error) response.apiError(error);
    if (!invoice) return response.apiNotFound();
    return response.apiResponse(invoice);
});

The problem is when I execute this function (findOnAndUpdate), the option runValidators: true doesn't work on taxes subDocuments but only on invoice fields??!!!



via medKHELIFI

No comments:

Post a Comment