2017-08-31 84 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