显示“无法读取null的属性'friends'的角度

时间:2019-05-29 16:33:53

标签: node.js angular httpclient

嗨,我正在尝试从我的nodejs后端获取数据到angular。我在nodejs中收到以下错误消息

Cannot read property 'friends' of null
    at UserModel.findOne.select.lean.exec

如果我尝试在邮递员中使用Api,效果很好。但是当在angular中使用时,我在nodejs中收到错误消息

服务器端代码为:

let findFriends = ()=>{
        return new Promise((resolve, reject)=>{
            UserModel.findOne({userId: req.body.userId || req.query.userId})
                .select('-__v -_id -password')
                .lean()
                .exec((err, userFriends)=>{
                    console.log(userFriends.friends.length)
                    if(err){
                        logger.error(err.message, ' friendsController: getAllNonFriends, findFriends', 5)
                        let apiResponse = response.generate(true, `Failed to Find Friends`, 500, null)
                        reject(apiResponse)
                    }else {
                        if(userFriends.friends.length !== 0){
                            resolve(userFriends.friends)
                        }else {
                            resolve(userFriends)
                        }  
                    }
                })
        })  
    } // end find requests

角度代码:

public allNonFriends: any = ()=>{
    this.http.getNonFriendsList(this.currentUser).subscribe(
      (apiResponse)=>{
        console.log(apiResponse)
        if(apiResponse.status === 200){
          console.log(apiResponse.data)
        } else {
          console.log(apiResponse.message)
        }
      },
      (err)=>{
        console.log(err)
      }
    )
  } // end all non friends

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。我将参数从body更改为query,并且有效