【问题标题】:How to update value inside object in firebase cloud firestore?如何更新 Firebase Cloud Firestore 中对象内部的值?
【发布时间】:2021-05-18 17:50:07
【问题描述】:

我有这个图像中的数据结构。我需要将布尔值从 true 更新为 false。

这是我执行更新的功能,但它不起作用...

worldIndex 和 levelIndex 一个数字。

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);
  }
});

我也试过这种方法,但它不起作用:

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);
  }
});

【问题讨论】:

  • 来人吗?=]

标签: javascript firebase react-native google-cloud-firestore


【解决方案1】:

要更新 firebase cloud firestore 中的字段,您必须编写

.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);
  }
});

【讨论】:

  • 嗨@Marci,当我尝试按照您的建议进行操作时,出现错误:[Error: TransformError SyntaxError: C:\Users\ionia\Desktop\quizAppNew\src\requests\userRequest.js:意外的令牌,预期的“,”(81:25)
  • 嘿@Michael 您有多个要更新的字段吗?如果是,则必须用逗号分隔字段。
  • 我只需要更新一个字段,但它非常非常嵌套在对象中。只需查看我代码中的示例即可。=]
  • 您可以尝试将 .data 添加到 gameData 和类似这样的问题:.update({gameData.data[worldIndex].questions.data[levelIndex].isLocked: false});我不确定这是否有助于选择正确的数据结构路径。
  • [TypeError: undefined is not an object (evalating '_gameData.default.data[worldIndex]')]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-18
  • 2018-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-18
相关资源
最近更新 更多