如何访问node.js中对象的属性

时间:2018-04-11 15:50:36

标签: node.js

我从节点中的api请求收到一个带有用户详细信息的json对象, 库使用自己的每个函数来遍历对象并提供详细信息。我试图使用(。)表示法访问对象中的特定属性。当我尝试user.profile时,我只获得了很好的配置文件,但是user.profile.UserProfile给了我undefined。我如何访问UserProfile? 这是迭代对象的代码。

const orgUsersCollection = client.listUsers();
console.log(orgUsersCollection);
orgUsersCollection.each(user => {
    console.log(user);
  })
  .then(() => console.log('All users have been listed'));

User {
  id: '',
  status: '',
  created: '',
  activated: null,
  profile:
   UserProfile {
     firstName: '',
     lastName: '',
     login: '',
     email: '' },
  
All users have been listed

1 个答案:

答案 0 :(得分:1)

对象的日志显得有些混乱。 'user.profile'中没有属性'UserProfile'。而你想写user.profile.firstName

let user = {
  id: '',
  status: '',
  created: '',
  activated: null,
  profile: {
     firstName: 'It Works!',
     lastName: '',
     login: '',
     email: ''
  }
};
console.log(user.profile.firstName)