node.js - TypeError: Cannot read property 'push' of undefined Mongoose -


i getting below error doing app in node.js using express. using mongoose db operations below have detailed design

party.js

var mongoose = require("mongoose");  var partyschema = new mongoose.schema({     partyname: string,     songs: [          {                  type: mongoose.schema.types.objectid,                 ref: "song"          }    ] });  module.exports = mongoose.model("party", partyschema); 

song.js

var mongoose = require("mongoose");  var songsschema = new mongoose.schema({     videoid: string,     videoname: string });  module.exports = mongoose.model("song", songsschema); 

my app.js

app.post("/search/addsong", function(req, res) {     party.find({partyname:"hello"},function(err,party){         if(err){             console.log("failed in find");         } else {             console.log(party);             // console.log(typeof(req.body.videoid));             var videoid = req.body.videoid;              var newsong = [ {                 videoid:req.body.videoid,                 videoname:req.body.videoname             }             ];             song.create(newsong, function(err, createdsong){        if(err){            console.log("error creating new party");        } else  {            console.log(createdsong);            party.songs.push(createdsong);// error on line            party.save();            res.redirect("/search");        }     });         }     });     res.render("addsong"); }); 

i able create collection objects of party , song individually, when add song party queue, following error:

typeerror: cannot read property 'push' of undefined 

can please let me know missing in here..!!

thanks in advance.!!

find returns cursor converted in array. if expecting single doc, call findone.

app.post("/search/addsong", function(req, res) {     party.findone({partyname:"hello"},function(err,party){         if(err){             console.log("failed in find");         } else {             console.log(party);             // console.log(typeof(req.body.videoid));             var videoid = req.body.videoid;              var newsong = [ {                 videoid:req.body.videoid,                 videoname:req.body.videoname             }             ];             song.create(newsong, function(err, createdsong){        if(err){            console.log("error creating new party");        } else  {            console.log(createdsong);            party.songs.push(createdsong);// error on line            party.save(function(err){              res.redirect("/search");            });        }     });         }     });     res.render("addsong"); }); 

also put redirect in callback of save, make sure redirect called when save completed, otherwise redirect without saving, giving asynchronous nature of javascript.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -