Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

javascript - 给定一组uid,如何在React Native中返回Firebase数据的JavaScript对象?(How can I return a JavaScript object of Firebase data in React Native, given an array of uids?)

I have an area of my code where users of a group are stored in an array.(我在代码区域中将组的用户存储在数组中。)

In some cases, I want to gather additional data on the users.(在某些情况下,我想收集有关用户的其他数据。) Here is what I tried so far:(这是我到目前为止尝试过的:) async getMembersData(groupUid) { const members = await this.getMembers(groupUid); var membersData = {}; Object.keys(members).map((key, i) => { const member = members[key]; firebase.database().ref(`users/${member.uid}`).once('value', snap => { membersData[member.uid] = { 'uid': member.uid, 'username': snap.child('username').val(), 'imageUrl': snap.child('imageUrl').val(), 'type': member.type, }; }); }); return membersData; } I have this code working similarly by setting state and using the firebase .on() function, but I need it to work with .once() and returning the object.(我可以通过设置状态并使用firebase .on()函数来使此代码类似地工作,但是我需要它与.once()一起工作并返回对象。)   ask by Robert Berge translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What about:(关于什么:)

async getMembersData(groupUid) { const members = await this.getMembers(groupUid); var membersData = {}; Object.keys(members).map((key, i) => { const member = members[key]; const snap = await firebase.database().ref(`users/${member.uid}`).once('value'); membersData[member.uid] = { 'uid': member.uid, 'username': snap.child('username').val(), 'imageUrl': snap.child('imageUrl').val(), 'type': member.type, }; }); }); return membersData; } Since the once() method returns a Promise with a DataSnapshot .(由于once()方法返回带有DataSnapshot的Promise。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...