首页 > 解决方案 > 从角度调用用户到节点(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 出现。如何在不将它们转换为普通对象的情况下获取整个数据?

标签: node.jsangularexpressmongoosemean

解决方案


尝试使用 res.json("JSON Object"); 而不是 res.send()..

否则尝试使用 robomongo 之类的工具检查数据库


推荐阅读