Sunday, 6 October 2019

Ripping out my hair here lol "Cannot read property 'push' of undefined" w/ MongoDB

​Beginner trying to work on a project for his potfolio, real stuck here---------------------------------------------------------------------------THE ROUTEHere, I am trying to set up a "private message" post route that finds both the user messaging and the user being messaged and pushes the new private message into the "private message" array of both users.app.post("/users/:id", function(req,res){user.find({id: req.params.id, id: req.user._id }).populate("privateMessage").exec(function(err, user){if(err){console.log(err);res.redirect("/home");}else{privateMessage.create(req.body.privateMessage, function(err, privateMessage){if(err){console.log(err);}else{privateMessage.author.id = req.user._id;privateMessage.author.username = req.user.username;privateMessage.userID= req.user._id;privateMessage.text=req.body.text;privateMessage.title= req.body.title;privateMessage.save();user.privateMessage.push(privateMessage);user.save();console.log(privateMessage);res.redirect('/home');}})}})})-----------------------------------------------------------Apparently user.privateMessage isn't defined. Here is the schema I am using for user​--------------------------------------------------------------------------------USER SCHEMAvar userSchema = new mongoose.Schema({password :String,username: String,bio: String,comment: [{type: mongoose.Schema.Types.ObjectId,ref: "comment"}],topic: [{type: mongoose.Schema.Types.ObjectId,ref: "topic"}],privateMessage: [{type: mongoose.Schema.Types.ObjectId,ref: "privateMessage"}]});​userSchema.plugin(passportLocalMongoose);​var user = mongoose.model("user", userSchema);-----------------------------------------------​I know the way I'm going about this may be stupid, but does anyone know what the discrepancy could be?

Submitted October 06, 2019 at 05:22PM by Papaya695

No comments:

Post a Comment