首页 > 解决方案 > GET 请求错误:postData.posts.map 不是函数

问题描述

我正在尝试从 mongodb 检索登录用户的特定运输信息。app.js 中的 console.log(req.params.id) 输出用户的 id,以便后端接收信息。`console.log(documents) in app.js returns correct information from database, but I get a messagepostData.posts.map 不是因为我的 service.ts 而抛出的函数"```

为什么会抛出这个错误?

配置文件.service.ts

  getShippingInformation(id: string) {

    return this.http
      .get<{ message: string; posts: any; maxPosts: number }>(
        `http://localhost:3000/api/users/${id}`
      )
      .pipe(
        map(postData => {
      console.log("postData");
      console.log(postData);
          return {
            posts: postData.posts.map(post => {
              return {
                fullName: post.fullName,
                address1: post.address1,
                address2: post.address2,
                city: post.city,
                state: post.state,
                zip: post.zip
              };
            }),
            maxPosts: postData.maxPosts
          };
        })
      );
  }

profile.component.ts

 this.profileService.getShippingInformation(this.userId).subscribe((res) => {

      this.shippingInfo = res.posts;
      console.log("shippinginfo");
      console.log(this.shippingInfo);

      if (res.posts[0].fullName === "Not Added Yet") {
        this.hideShippingData = false;

      }
      else {
        this.hideShippingData = true;
        this.fullName = res.posts[0].fullName;
        this.address1 = res.posts[0].address1;
        this.address2 = res.posts[0].address2;
        this.city = res.posts[0].city;
        this.state = res.posts[0].state;
        this.zip = res.posts[0].zip;
      }
      this.isLoading = false;
    });

应用程序.js

app.get("/api/users/:id", (req, res, next) => {
console.log("req.params.id");
console.log(req.params.id);
  if (req.params.id) {
    console.log("MADE IT TO USER SECTION");
    console.log(req.params.id);
  }
  User.findById({ _id: req.params.id })
    .select("fullName address1 address2 city state zip")
    .then(documents => {
     console.log(documents);
      res.status(200).json({
        message: "User account located!",
        posts: documents
      });
    });
});

在此处输入图像描述

标签: node.jsangularmongodbexpressmongoose

解决方案


推荐阅读