Syed Ayesha Bebe
Syed Ayesha Bebe

Reputation: 1448

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

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);

Upvotes: 7

Views: 19744

Answers (4)

achyuth
achyuth

Reputation: 11

we get this error when we do not import the router properly.

the example code snippet

const express = require('express')
const router = express.Router();

router.get("/signout", (req, res) =>{
  res.send("user sign out")
 })


module.exports = router;

Upvotes: 0

Milan Etrich
Milan Etrich

Reputation: 67

Router();

In this method If we are not mention () this also it will show the push error...

const express = require('express');
const routerd = express.Router();
const Ninja = require('./models/ninja');

routerd.delete('/ninja:/id', function(req, res, next){
    console.log(req.params.id);
    res.send({type : 'DELETE'});
}).catch(next);

module.exports = routerd;

Upvotes: 7

luky
luky

Reputation: 212

This can also be solved as:

Batches.findById(req.params.batchId, function (err, batch) {
    if (err) throw err;
  const a = req.body.venue;
  for(i=0;i<a.length;i++ )
  {
   batch.venue.push(req.body.venue[i]);
  }

    batch.save(function (err, batch) {
        if (err) throw err;
        console.log('Updated venue!');
        res.json(batch);
    });
});

Upvotes: 1

rsp
rsp

Reputation: 111506

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.

Upvotes: 3

Related Questions