Michael
Michael

Reputation: 439

How to update value inside object in firebase cloud firestore?

I have data structure like in this image. I need to update boolean value from true to false.

This is my function to perform the update, but it's not working...

The worldIndex and levelIndex a numbers.

export const setLevelPassed = (worldIndex, levelIndex) => new Promise(async (resolve, reject) => {
  try {
    await firestore()
      .collection(auth().currentUser.uid)
      .doc('gameDetails')
      .update(gameData[worldIndex].questions[levelIndex].isLocked = false);
    resolve();
  } catch (e) {
    reject(e);
    console.log('setLevelPassed', e);
  }
});

enter image description here

I tried this way too , but it's not working:

export const setLevelPassed = (worldIndex, levelIndex) => new Promise(async (resolve, reject) => {
  try {
    await firestore()
      .collection(auth().currentUser.uid)
      .doc('gameDetails')
      .update(`gameData[${worldIndex}].questions[${levelIndex}].isLocked`, false);
    resolve();
  } catch (e) {
    reject(e);
    console.log('setLevelPassed', e);
  }
});

Upvotes: 0

Views: 184

Answers (1)

Marci
Marci

Reputation: 314

To update a field in firebase cloud firestore you must write

.update({ field : value, field : value, ... })

export const setLevelPassed = (worldIndex, levelIndex) => new Promise(async (resolve, reject) => {
  try {
    await firestore()
      .collection(auth().currentUser.uid)
      .doc('gameDetails')
      .update({gameData[worldIndex].questions[levelIndex].isLocked: false});
    resolve();
  } catch (e) {
    reject(e);
    console.log('setLevelPassed', e);
  }
});

Upvotes: 1

Related Questions