MEAN 堆栈 http.post 请求正文为空

MEAN stack http.post request body is empty

我正在 mean stack 中创建一个项目,我 运行 进入以下问题。

当我尝试将某些内容上传到我的数据库时,由于请求正文为空,http.post 请求无法正常工作,因此我在 mongoose 中收到了一个 ValidationError 以获取它应该需要的参数,但是没有得到。

所以我首先使用 service.ts 来提供从 html 文件中的表单获得的参数。为此,我正在使用响应式表单和 FormData 对象。

addProp(address: string, type: string, size: number, condition: string, year: number,
    numberOfRooms: number, moveableFrom: string, furnitured: boolean, garden: boolean, attic: boolean, pet: boolean,
    smoke: boolean, heatingType: string, elevator: boolean, level: number) {
    

    const propData = new FormData();
    propData.append("address", address);
    propData.append("type", type);
    propData.append("condition", condition);
    propData.append("year", year as unknown as string);
    propData.append("numberOfRooms", numberOfRooms as unknown as string);
    propData.append("moveableFrom", moveableFrom);
    propData.append("furnitured", furnitured as unknown as string);
    propData.append("garden", garden as unknown as string);
    propData.append("attic", attic as unknown as string);
    propData.append("pet", pet as unknown as string);
    propData.append("smoke", smoke as unknown as string);
    propData.append("heatingType", heatingType as unknown as string);
    propData.append("size", size as unknown as string);
    propData.append("elevator", elevator as unknown as string);
    propData.append("level", level as unknown as string);
    this.http
      .post<{ message: string; prop: Property }>(
        "http://localhost:3000/api/props",
        propData
      )
      .subscribe(responseData => {
        this.router.navigate(["/"]);
      });
  }

然后去后台的post请求properties.js

router.post(
  "",
  req => {
    const prop = new Property({
      address: req.body.address,
      type: req.body.type,
      size: req.body.size,
      condition: req.body.condition,
      year: req.body.year,
      moveableFrom: req.body.moveableFrom,
      numberOfRooms: req.body.numberOfRooms,
      furnitured: req.body.furnitured,
      elevator: req.body.elevator,
      level: req.body.level,
      garden: req.body.garden,
      attic: req.body.attic,
      pet: req.body.pet,
      smoke: req.body.smoke
    });
    prop.save();.then(updatedProperty => {
      res.status(201).json({
        message: "Property added successfully",
        prop: {
          ...updatedProperty,
          id: updatedProperty._id
        }
      });
    });
  }
);

当我 console.log 这里的 req.body 我会得到一个空对象,但是当我 conole.log 它在 service.ts 文件的前端时它工作正常.

什么可能导致此问题?

Post 数据作为 JSON 对象,而不是作为 FormData 对象发送。