samedi 9 mai 2020

If statement reversibly executing instructions in node js with express and mongoDB

This is my code snippet, Whenever I tried to execute the code it's not going inside the if statement despite the value of listName is exactly matching with the if condition. Also in the List.findOne() it's also every time returning null. My

app.post("/", function (req, res) {
  const itemName = req.body.newItem;
  const listName = req.body.list;
  const item = new Item({
    name: itemName
  });
  console.log(listName);
  if (listName === 'Today') {
    console.log("Entered");
    item.save();
    res.redirect("/");
  } else {
    List.findOne({
        name : listName
      }, function(err,foundList){
          console.log(foundList);
          foundList.items.push(item);
          foundList.save();
          res.redirect("/" + listName);
      });
    }
});

you can check what is actually being returned by terminal.

Server started on port 3000
Today 
null
events.js:288
      throw er; // Unhandled 'error' event
      ^

TypeError: Cannot read property 'items' of null

thanks in advance!

Aucun commentaire:

Enregistrer un commentaire