0

I have a user schema including a few sub-schemas. I have 2 similar sub-schemas, one of them is recognised but the other one is undefined all the time. my user schema is as follows:

var UserSchema = new Schema({

            company: String,
            resetPasswordToken: String,
            resetPasswordExpires: Date,
            isAdmin: {type: Boolean, default: false},

            projects: [
                {
                    type: mongoose.Schema.Types.ObjectId,
                    ref: "Project",
                }
            ],
            trackers: [
                {
                    type: mongoose.Schema.Types.ObjectId,
                    ref: "Tracker",
                }
            ],


});

In the above schema, I have projects that is working very well in a post route . The form gets posted, saved and shown properly but the same route for trackers cannot be posted and give "trackers is undefined" error:

project post route:

app.post("/myprojects", function(req,res){

User.findById(req.user._id, function(err, user){
        if(err){
            console.log(err);
            }else{

                var pname = req.body.pname;
                var pnumber = req.body.pnumber;

                 var newProject = {
                    pname:pname,
                    pnumber:pnumber, 

                 };

                Project.create(newProject, function(err, project){
                    if(err){
                        console.log(err);
                           }else{
                                user.projects.push(project);
                                user.save();
                                res.redirect("/myprojects");


                                }
                });
            }
    });
});

Here is the tracker post:

app.post("/tracker", function(req,res){

User.findById(req.user._id, function(err, user){
        if(err){
            console.log(err);
            }else{

                var tname = req.body.tname;
                var tnumber = req.body.tnumber;

                 var newTracker = {
                    tname:tname,
                    tnumber:tnumber, 

                 };

                Tracker.create(newTracker, function(err, tracker){
                    if(err){
                        console.log(err);
                           }else{
                                user.trackers.push(tracker);
                                user.save();
                                res.redirect("/tracker");

                                }
                });
            }
    });
}); 

Please note both projects schema and tracker schemas are saved in a model folder and been required in app.js

photo of the error: enter image description here

Sam
  • 197
  • 1
  • 11
  • It can happen, and the reason why is because a document somehow got created without putting a default empty array `[]` assigned to the `trackers` property. For that and a host of other reasons you really should not do that and instead use the `$push` operator of MongoDB. It's safe and atomic and does not care if there is no such property in the document. – Neil Lunn Jun 06 '18 at 07:04
  • Sorry what should I do? I couldn’t get what you meant – Sam Jun 06 '18 at 07:22
  • Move your eyes up from the comment to the large box above your question and click on the link. That explains what to do. – Neil Lunn Jun 06 '18 at 07:25
  • I found it. Thanks – Sam Jun 06 '18 at 07:28
  • I used the link and with a bit of change it worked very well. Thank you – Sam Jun 07 '18 at 02:12

1 Answers1

0

It must be giving you undefined because trackers is not saved in database on user object. You can use the following to fix the problem.

user.trackers = user.trackers || [];
user.trackers.push(tracker);
Farhan Tahir
  • 2,096
  • 1
  • 14
  • 27