简体   繁体   中英

How do I use a document's field from one collection to retrieve another document field from a different collection?

Here is how my database is structured: challenges table & users table

Here is the error I'm getting: error image

I want to use the "created_by" field which is also a document id for the users table, where I want to retrieve both the display name and the photo URL.

I'm not all the way sure how promises work and I have a feeling that is why I'm struggling, but the code I have thus far is below:

Data Retrieval:

UsersDao.getUserData(ChallengesDao.getChallenges().then(result => {return result['author'][0]})).then(result => {console.log(result)})

Challenges DAO:

export default class ChallengesDao {

  static async getChallenges() {
const db = require('firebase').firestore();
    // const challenges = db.collection('challenges').limit(number_of_challenges)
    // challenges.get().then(())

    const snapshot = await db.collection('challenges').get()

    const names = snapshot.docs.map(doc => doc.data().name)
    const createdBy = snapshot.docs.map(doc => doc.data().created_by)
    const highScores = snapshot.docs.map(doc => doc.data().high_score.score)
    return {challengeName: names, author: createdBy, score: highScores}
  }

Users DAO:

const db = require('firebase').firestore();

export default class UsersDao {
  static async getUserData(uid: string) {
    let userData = {};
    try {
      const doc = await db
        .collection('users')
        .doc(uid)
        .get();
      if (doc.exists) {
        userData = doc.data();
      } else {
        console.log('User document not found!');
      }
    } catch (err) {}
    return userData;
  }
}

You're getting close. All that's left to do is to call getUserData for each UID you get back from getChallenges .

Combining these two would look something like this:

let challenges = await getChallenges();
let users = await Promise.all(challenges.author.map((uid) => getUserData(uid));

console.log(challenges.challengeName, users);

The new thing here is Promise.all() , which combines a number of asynchronous calls and returns a promise that completes when all of them complete.


Your code looks a bit odd to me at first, because of the way you return the data from getChallenges . Instead of returning three arrays with simple values, I'd recommend returning a single array where each object has three values:

  static async getChallenges() {
    const db = require('firebase').firestore();
    const snapshot = await db.collection('challenges').get();

    const challenges = snapshot.docs.map(doc => { name: doc.data().name, author: doc.data().created_by, score: doc.data().high_score.score });

    return challenges;
  }

If you then want to add the user name to each object in this array, in addition to the UID that's already there, you could do:

let challenges = await getChallenges();
await Promise.all(challenges.forEach(async(challenge) => {
  challenge.user = await getUserData(challenge.author);
});

console.log(challenges);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM