Dor Moshkovitz
Dor Moshkovitz

Reputation: 371

Mongoose reports no error on updating, but does not update

losing my mind here for something for a MongoDB document update with Mongoose, not reporting any error but not actually updating successfully.

I have this schema:

/**
 * Branch Schema
 */
let BranchSchema = new Schema({
  name: String,
  domain: String,
  email: String,
  bm: { type: Schema.ObjectId, ref: 'User' },
  st: [{ type: Schema.ObjectId, ref: 'User' }],
  stockCurrent: {
    paper: Schema.Types.Object,
    ink: Schema.Types.Object
  },
  stockNeeded: {
    paper: Schema.Types.Object,
    ink: Schema.Types.Object
  },
}, { versionKey: false, usePushEach: true });

mongoose.model('Branch', BranchSchema);

Trying to update stockCurrent, using this logic:

Branch.findById(config.branch.id, function (err, branch) {
  if (err) {
    res.status(422).send({
      message: 'הסניף לא נמצא'
    });
  } else {
    console.log(branch);
    Object.keys(req.body.stock).forEach(function (type) {
      Object.keys(req.body.stock[type]).forEach(function (code) {
        if (req.body.stock[type][code] > 0) {
          if (typeof branch.stockCurrent[type][code] === 'undefined') {
            branch.stockCurrent[type][code] = 0;
          }
          branch.stockCurrent[type][code] += req.body.stock[type][code];
        }
      });
    });
    console.log(branch);

    branch.save(function (err, updated) {
      console.log("err: " + err);
      if (err) {
        stock.remove();
        res.status(422).send({
          message: 'שגיאה בשמירת מלאי'
        });
      } else {
        console.log(updated);
        res.send({
          message: 'מלאי נוסף בהצלחה'
        });
      }
    });
  }
});

I get to to success part, having my console log this:

{
   "_id":5dd276a6bcc29a13789fcecb,
   "name":"בצלאל ארכיטקטורה",
   "domain":"bezalel.eazix.io",
   "email":"[email protected]",
   "bm":5cdd2130d192ea03a87d2dfd,
   "stockNeeded":{
      "ink":{
         "GY":2,
         "PM":2,
         "M":2,
         "MBK":2,
         "PBK":2,
         "PC":2,
         "Y":2,
         "C":2,
         "waste":2
      },
      "paper":{
         "COATED":5,
         "PLAIN":5,
         "PHOTO":3
      }
   },
   "stockCurrent":{
      "paper":{
         "PLAIN":0
      },
      "ink":{
         "waste":0
      }
   },
   "st":[

   ]
}{
   "_id":5dd276a6bcc29a13789fcecb,
   "name":"בצלאל ארכיטקטורה",
   "domain":"bezalel.eazix.io",
   "email":"[email protected]",
   "bm":5cdd2130d192ea03a87d2dfd,
   "stockNeeded":{
      "ink":{
         "GY":2,
         "PM":2,
         "M":2,
         "MBK":2,
         "PBK":2,
         "PC":2,
         "Y":2,
         "C":2,
         "waste":2
      },
      "paper":{
         "COATED":5,
         "PLAIN":5,
         "PHOTO":3
      }
   },
   "stockCurrent":{
      "paper":{
         "COATED":1,
         "PHOTO":2,
         "PLAIN":0
      },
      "ink":{
         "PM":1,
         "waste":0
      }
   },
   "st":[

   ]
}**"err":null**{
   "_id":5dd276a6bcc29a13789fcecb,
   "name":"בצלאל ארכיטקטורה",
   "domain":"bezalel.eazix.io",
   "email":"[email protected]",
   "bm":5cdd2130d192ea03a87d2dfd,
   "stockNeeded":{
      "ink":{
         "GY":2,
         "PM":2,
         "M":2,
         "MBK":2,
         "PBK":2,
         "PC":2,
         "Y":2,
         "C":2,
         "waste":2
      },
      "paper":{
         "COATED":5,
         "PLAIN":5,
         "PHOTO":3
      }
   },
   "stockCurrent":{
      "paper":{
         "COATED":1,
         "PHOTO":2,
         "PLAIN":0
      },
      "ink":{
         "PM":1,
         "waste":0
      }
   },
   "st":[

   ]
}

I can see the here the initial state, the updated version before saving, and the the err:null, and the allegedly updated document.

but alas! the document wasn't really updated. it remains the same.

I have tried many things, searching and looking for similar cases, checking my schema, adding useStrict:false to the schema, nothing helps.

Mongoose ver 4.13.20, Mongodb ver 3.6.17

SOS

Dor

Upvotes: 1

Views: 672

Answers (1)

djs
djs

Reputation: 4065

I'm guessing the SchemaTypes are the problem? In Mongoose 4.x, these are the only valid SchemaTypes:

String
Number
Date
Buffer
Boolean
Mixed
Objectid
Array

Notice that Mixed is an option but not Object. You need to tell Mongoose that you updated a Mixed field using model.markModified('pathName'). See the Mixed docs.

So in your case, the code below may fix the issue:

branch.markModified('stockCurrent');
branch.save(function (err, updated) {
// ...

Upvotes: 3

Related Questions