Sunday 12 March 2017

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" so how can I solve this? Here my schema code with the help of these I gave values to parent schema but I cannot 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 the routing code is

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 error at batch.venue.push(req.body); like what the problem I mention in the above



via Syed Ayesha Bebe

No comments:

Post a Comment