node.js - 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 message
postData.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
});
});
});
解决方案
推荐阅读
- optimization - 如何以最优方式移动大型阵列 n 发生次数
- c# - 在具有多个条件的 linq 中获取匹配的属性名称
- typescript - 每分钟将字段值增加 1
- spring-boot - 面临问题在 Spring Boot 中配置 Spring Batch Admin-无法解析名称“集成:smartLifeCycleAttributeGroup”
- c# - 通过使用 Lambda 表达式的 Where 子句连接超过 2 或 3 个表。然后将对象发送到视图
- ionic3 - 离子3和离子4的主要区别是什么
- javascript - 使用 Math.max 和 Math.min 从数组中删除最高和最低数字
- unity3d - 如何在Unity中获得2个四元数的相对旋转
- google-maps - 用户分享位置时如何获取国家、城市、州和地区信息等参数...?
- python - 从文本中删除正则表达式数字