node.js - 从角度调用用户到节点(MEAN)仅向角度发送电子邮件和 _id
问题描述
我正在使用以下代码..
// Node.js
router.get('/users/all', authenticate, (req, res) => {
User.find({some query}).select('firstName lastName email').then((users) => {
res.send(users);
});
//Angular Service
let opts = { headers: this.getCommonHeaders(), params: new HttpParams() };
return this.http.get(getUserDataUrl, opts)
.pipe(
map(response => {
// here is the problem
console.log(response.json());
return response.json()
}),
catchError(this.handleErrors)
);
在 opts 标题中,我提到了数据类型:
getCommonHeaders() {
return new HttpHeaders({
'Content-Type': 'application/json',
'x-auth': this.getToken()
})
}
在 console.log(response.json()) 中,我在数组对象中只收到电子邮件和 _id。在 res.send(user) 之前在 node.js 上添加相同的 console.log(user) 会显示包含 firstName 和 lastName 的整个数据
然后我将 .lean() 添加到 User 函数中。这允许其余的 firstName 和 lastName 出现。如何在不将它们转换为普通对象的情况下获取整个数据?
解决方案
尝试使用 res.json("JSON Object"); 而不是 res.send()..
否则尝试使用 robomongo 之类的工具检查数据库
推荐阅读
- java - 在 GLFW Open Gl 中,其创建的帧缓冲区在屏幕拉伸时不会改变大小
- azure - 当 AppService 计划自动扩展时,HostedService 是否会自动扩展?
- three.js - three.js 用球体连接两个运动部件
- python - 当我调用 list.count() 函数时出现 AttributeError
- rust - 在 Substrate 的 decl_storage 宏中 impl trait X for Y as Z 是什么意思?
- mysql - SQL - 拥有 COUNT() 不能按预期工作
- python - 从 __init__.py 中的子模块导入函数而不暴露子模块
- python - 模型关系 - ForeignKey 或 OneToMany?
- c - 信用卡号码验证
- scala - 如何在不使用 UDF 的情况下将字符串列附加到 Scala Spark 中的数组字符串列?