How to solve cannot read property 'push' of undefined in nodejs application?

Syed Ayesha Bebe picture Syed Ayesha Bebe · Mar 13, 2017 · Viewed 10.1k times · Source

While I am adding values to a sub document, in command prompt it shows the error
like cannot read property push. How can I solve this?

Here's my schema code with the help of these I gave values to parent schema
but I'm not able to give the values to this sub document:

var venueSchema = new Schema({
    name:  {
        type: String,
        required: true,
        unique:true
    },
    address:  {
        type: String,
        required: true
    }
}, {
    timestamps: true
});

// create a schema
var batchSchema = new Schema({
   batchname: {
        type: String,
        required: true,
        unique: true
    },
    activityname: {
        type: String,
        required: true
    },
    time: {
    type:String,
    required:true
    },
    duration: {
    type:String,
    required:true
    },
    classtype: {
    type:String,
    required:true
    },
    trainer: {
    type:String,
    required:true
    },
    price:{
    type:Currency,
    required:true,
    unique:true
    },

    venue:[venueSchema]
}, {
    timestamps: true
});  

And my Routing code:

batchRouter.route('/:batchId/venue')
.post(function (req, res, next) {
    Batches.findById(req.params.batchId, function (err, batch) {
        if (err) throw err;
      batch.venue.push(req.body);
        batch.save(function (err, batch) {
            if (err) throw err;
            console.log('Updated venue!');
            res.json(batch);
        });
    });
})

Here parent document is batchSchema and sub-document is venueSchema. After
creation of batch I will get an id. With the help of this id I am trying to add values to venue at that time it shows me the error at
batch.venue.push(req.body);

Answer

rsp picture rsp · Mar 13, 2017

The error that you get means that:

  1. You don't get an error from the database because if (err) throw err; doesn't fire
  2. Your batch.venue is undefined because you get Cannot read property 'push' of undefined
  3. Your batch is defined because you don't get Cannot read property 'venue' of undefined

It means that you have a connection with the database, you get a document with the ID that you want, but it doesn't have the property venue that you expect to be present and to be an array.

Instead of:

batch.venue.push(req.body);

you can use:

if (!Array.isArray(batch.venue)) {
    batch.venue = [];
}
batch.venue.push(req.body);

or:

if (Array.isArray(batch.venue)) {
    batch.venue.push(req.body);
} else {
    batch.venue = [req.body];
}

or something like that, i.e. you need to check if you have an array before you try to push elements to it. If you don't have an array then you will have to create a new one.