Skip to content
Advertisement

How to combine different paths in firestore version 9

I was using firestore version 8 and used the following code to create alternations in code:

const subQuestionRef = DB.collection('groups')
                         .doc(groupId)
                         .collection('questions')
                         .doc(questionId)
                         .collection('subQuestions')

if(someCondition) {
  subQuestionRef.doc(uid).set({somthing})
} else {
  subQuestionRef.doc(subQuestionId).set({somthingElse})
}

I couldn’t find a way to create such alternations in firestore version 9.

Do you know how to create alternations in version 9?

Advertisement

Answer

the way I would approach this with V9 will be the follow:

const subQuestionRef = collection(db, `groups/${groupId}/questions/${questionId}/subQuestions`)

if (someCondition) {
  setDoc( doc(subQuestionRef, uid), {something} )
} else {
  setDoc( doc(subQuestionRef, subQuestionId), {somethingElse} )
}

We now have a really nice API reference in V9.

doc() reference: https://firebase.google.com/docs/reference/js/firestore_.md#doc

Advertisement