Search code examples
node.jsexpressmongooseexpress-session

Express session property not setting in schema


I'm trying to use an express-session property to set a value in a Mongoose document, however whenever I do it returns a "Path ownerId is required".

I've tried logging the variable to the console, both before and after the mongoose Model is created.

router.post('/create', (req, res) => {
  console.group();
  var propertyId = randomString(11);

  var ownerId = req.session.userId;

  console.log("ownerId value: " + ownerId);

  var newProperty = new Property({
    _id: propertyId,
    owner: ownerId,
    address: req.body.address,
    landline: req.body.landline
  });

  console.log("ownerId value: " + ownerId);

  Property.create(newProperty, (err, property) => {
    if (err) {
      res.send(err);
    } else {
      res.send(property);
    }
  });
  console.groupEnd();
});

I expected both console.log's to output the ID, which they did, but I also expected the newUser object to contain the ID, but since I got a validator error on the Property.save function, I know that it didn't.


Solution

  • Path ownerId is required message

    This mean in your table schema (Property scheme) has defined a filed called - ownerId, but when you create a new Property, input data does not include ownerId field.

    I see your input data includes owner: ownerId,, just check again your table schema.