如果验证应该没问题,如何修复 POST "ValidationError"?

How to fix the POST "ValidationError" if the validation is supposed to be ok?

我正在制作一个送货应用程序。 当我尝试 POST Postman 中包含所有必需数据的订单请求时,每个对象都出现以下错误:

"message": "order validation failed: recipient.recLocation.recPhone: Path `recipient.recLocation.recPhone` is required., recipient.recLocation.recZipcode: Path `recipient.recLocation.recZipcode` is required., recipient.recLocation.recStreetnumber: Path `recipient.recLocation.recStreetnumber` is required., recipient.recLocation.recStreet: Path `recipient.recLocation.recStreet` is required., recipient.recLocation.recCity: Path `recipient.recLocation.recCity` is required., recipient.recName: Path `recipient.recName` is required.",

所以基本上,none 我的请求通过了。我环顾四周,根据我在此处和 Google 上以前的答案中找到的内容,我的猫鼬模型和路由文件似乎都非常好。 这是 "recipient":

模型的片段

recipient: {
    //rec in front of field names stands for "recipient" - done so that the data of recipient doesn't get confused for the data of the sender
    recName: {
      type: String,
      required: true
    },
    recLocation: {
      recCity: {
        type: String,
        required: true,
        max: 32
      },
      recStreet: {
        type: String,
        required: true,
        max: 32
      },
      recStreetnumber: {
        type: String,
        required: true,
        max: 16
      },
      recZipcode: {
        type: String,
        required: true,
        max: 16
      },
      recPhone: {
        type: String,
        required: true,
        max: 32
      }
    }
  }

这是路由文件中的片段:

    const orderFields = {};
    orderFields.user = req.user.id;
    if (req.body.description) orderFields.description = req.body.description;
    if (req.body.status) orderFields.status = req.body.status;
    //recipient
    orderFields.recipient = {};
    if (req.body.recName) orderFields.recipient.recName = req.body.recName;
    if (req.body.recCity) orderFields.recipient.recCity = req.body.recCity;
    if (req.body.recStreet)
      orderFields.recipient.recStreet = req.body.recStreet;
    if (req.body.recStreetnumber)
      orderFields.recipient.recStreetnumber = req.body.recStreetnumber;
    if (req.body.recZipcode)
      orderFields.recipient.recZipcode = req.body.recZipcode;
    if (req.body.recPhone) orderFields.recipient.recPhone = req.body.recPhone;

我猜我设置路径的方式是错误的,但我是根据我通过谷歌搜索和探索这里的其他问题所发现的来完成的。

提前致谢!

编辑

This is what I'm trying to pass in Postman

此外,当我将上述代码段从路由文件更改为:

    orderFields.recipient = {};
    if (req.body.recName) orderFields.recipient.recName = req.body.recName;
    if (req.body.recCity)
      orderFields.recipient.recLocation.recCity = req.body.recCity;
    if (req.body.recStreet)
      orderFields.recipient.recLocation.recStreet = req.body.recStreet;
    if (req.body.recStreetnumber)
      orderFields.recipient.recLocation.recStreetnumber =
        req.body.recStreetnumber;
    if (req.body.recZipcode)
      orderFields.recipient.recLocation.recZipcode = req.body.recZipcode;
    if (req.body.recPhone)
      orderFields.recipient.recLocation.recPhone = req.body.recPhone;

我得到一个不同的错误: 2nd Postman screenshot

有文档齐全的可用包,Express 验证器非常容易实现。您不需要在函数内部创建大的 if 条件。 请关注thislink.

您似乎应该传递具有层次结构主体的请求,而不是扁平化字段。 recipient 模型描述了一个具有 5 个必填字段的 recLocation,您确实提供了它们 – 但在错误的位置。

尝试 post 一个 JSON 请求同样描述位置:

recLocation: {
   reqCity: "..."
   reqStreet: "..."
   ...
}