首页 > 解决方案 > Express 会话属性未在架构中设置

问题描述

我正在尝试使用express-session属性在 Mongoose 文档中设置值,但是每当我这样做时,它都会返回“ownerId需要路径”。

在创建猫鼬模型之前和之后,我都尝试将变量记录到控制台。

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();
});

我希望两者都console.log输出 ID,他们确实这样做了,但我也希望newUser对象包含 ID,但由于我在Property.save函数上遇到验证器错误,我知道它没有。

标签: node.jsexpressmongooseexpress-session

解决方案


Path ownerId is required信息

这意味着在您的表模式(Property方案)中定义了一个名为 -的字段ownerId,但是当您创建一个新的 时Property,输入数据不包括ownerId字段。

我看到您的输入数据包括owner: ownerId,,只需再次检查您的表架构。


推荐阅读