2017-08-31 83 views
0

是否有可能以这种方式找到用户和抓取信息?试图流星+ React + createContainer - 用户资料查找

console.log(this.props.userDetails) 

这里的时候,我得到unidentified是我createContainer配置

export default createContainer(() => { 

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

    return { userDetails }; 
}, userProfile); 

假设一个有效的ID将被传递和结果将作为道具USERPROFILE组件

回答

0

待办事项你的意思是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); 

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

+1

正确的答案,你也可以解释,那'找到()'光标返回从查询找到的所有文件。在该游标上调用fetch()会创建一个文档数组,即光标指向的数组。 Noe在该数组上调用'.profile()'返回(可以理解)'undefined'。只是为了让OP明白这个'undefined'是如何创建的以及底层机制是什么。 – Jankapunkt