Thursday, 11 May 2017

Why is property null if its being recorded?

Just got into node.js and I appear to have run into an issue with a middleware that will authenticate the user to page containing user information specific to that user. I have body-parser and passport-local(/mongoose) installed as well as well as requiring it. When I create a project, I console logged the founder as well as checked the mongo db, so I know that its being recorded. Here is some of my code:

    app.use(bodyParser.urlencoded({extended:true}));
    app.use(function(req, res, next){
    res.locals.currentUser = req.user;
    next();
    });

The route that I am trying to get, the one it's erroring out on:

    app.get("/create/myProjects", middleWare.checkProjectOwnership, 
    function(req, res) {
    Project.getById(req.params._id, function(err, foundProjects){
       if (err) {
          res.redirect("back");
       } else
       {
       res.render("myProjects");
       }
     });
    });

The middleware (seems to error OUT on bolded line:

      middlwareObj.checkProjectOwnership = function(req, res, next){

        if (req.isAuthenticated()){
          Project.findById(req.params._id, function(err, foundProject){
          if (err) {
            req.flash("error", "Project not found");
            res.redirect("back");
          } else {
             if (**foundProject.founder.id.equals(req.user._id)**){
                console.log(req.user._id);
                next();
          } else {
              req.flash("error", "You don't have permission to view that");
            res.redirect("back");
          }
        }
        });
        } else {
        req.flash("error", "You need to be logged in to do that");
        res.redirect("back");
        }
       };

and the model its referencing:

   var projectSchema = new mongoose.Schema({
   name: String,
   description: String,
   founder: {
      id: {
          type: mongoose.Schema.Types.ObjectId,
          ref: "User"
      },
     username: String
    },
   });



via dpat0074