【问题标题】:Meteor + React + createContainer - User Profile LookupMeteor + React + createContainer - 用户资料查找
【发布时间】:2017-08-31 03:35:01
【问题描述】:

是否可以通过这种方式找到用户并获取信息?我在尝试时收到unidentified

console.log(this.props.userDetails)

这是我的 createContainer 配置

export default createContainer(() => {

  const userDetails = Meteor.users.find({ _id: 'WSLuMFqofmks5i99F'  }).fetch().profile

  return { userDetails };
}, userProfile);

假设将传递一个有效的 ID,并且结果将作为 props 提供给 userProfile 组件

【问题讨论】:

    标签: reactjs meteor


    【解决方案1】:

    你的意思是undefined? 确切的错误消息会有所帮助。

    您可以使用findOne() 代替find()fetch()(只是为了方便)。

    这行代码:

    const userDetails = Meteor.users.find({ _id: 'WSLuMFqofmks5i99F'  }).fetch().profile
    

    假设存在具有该 _id 的用户。更好的防御代码是:

    export default createContainer(() => {
      const user = Meteor.users.findOne({ _id: 'WSLuMFqofmks5i99F'  })
      if (!user) throw new Meteor.error("Could not find user")
      return {userDetails: user.profile}
    }, userProfile);
    

    然后你可以看到错误是什么

    【讨论】:

    • 正确答案,您也可以解释,find() 将光标返回到从查询中找到的所有文档。在该游标上调用 fetch() 会创建一个文档数组,即游标指向的。 Noe 在该数组上调用 .profile() 返回(可以理解)undefined。只是为了让 OP 了解这个 undefined 是如何创建的以及底层机制是什么。
    猜你喜欢
    • 2017-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    • 2017-04-10
    • 2017-05-15
    • 2011-03-14
    • 1970-01-01
    相关资源
    最近更新 更多