0
votes

hopefully this isnt too hard to solve.

TLDR; Mostly the question is how to use async/await instead of then() syntax. More specifically how can I access docRef of the accessed document to return the id of that document along with all the other data?


I have a series of words objects that I retrieve from my firestore database.

Each of these words is an object that looks like this: {word: 'Apple', ref: 9IOGPbdfoURivbNfffwF, points: 10}

I iterate through each of these words to capture their full breakdown, provided at the destination of the ref. Each of these words, may or may not, include a translations sub-collection.


unfortunately, while I can successfully return wordData, that doc does not include the ID of the word. Rather, the ID I need is the ID of the word document itself. How can I write this so I have a word object that has all the word data + the ID of the doc using async/await syntax?

const data = Promise.all(
      vocab.map(async (word: Word) => {
        const wordRef = await word.ref.get();
        const wordData = await wordRef.data();     <-- Problem is here.
 
        const docRef = firebaseFirestore
          .collection(`languages/${language}/vocabulary`)
          .doc(wordData.id)                        <-- What i'm trying to do, but id isn't there
          .collection('translations')
          .doc(translationLanguage);
    ...
)

Thanks!

1

1 Answers

1
votes

data() just returns the fields in the document. It doesn't return the ID. You already have the ID of the document in word.ref.

const id = word.ref.id;

Also, there is no need to use await with data(), since it doesn't return a promise.