【问题标题】:how to update values in Firestore database如何更新 Firestore 数据库中的值
【发布时间】:2021-07-06 07:00:45
【问题描述】:

我正在尝试更新 Firestore 数据库中的值 (isComplete)。我不知道文档 ID,但我正在尝试更新“description”= 123 和“remindTime”= 11 am 的值

const todoRef = db.collection("todo");

const isComplete = (date) => {
  return todoRef
    .where("description", "==", 123)
    .where("remindTime", "==","11 am")
    .update({
      isComplete: true,
    });
};

【问题讨论】:

  • 我建议将您的函数从 isComplete 重命名为 markAsCompleted 或类似名称。

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


【解决方案1】:

您需要知道文档 ID(或拥有reference)才能更新它。试试下面的函数:

const todoRef = db.collection("todo");

const isComplete = async (date) => {
  const todoDoc = await todoRef
    .where("description", "==", 123)
    .where("remindTime", "==","11 am")
    .get()
  if (todoDoc.empty) return "No TODO found"
  return todoDoc.docs[0].ref.update({isComplete: true})
};

【讨论】:

  • return await 是多余的,直接用return就好了。此外,您需要使用todoDoc.docs[0],因为todoDoc[0] 将是undefined
猜你喜欢
  • 2022-01-25
  • 2021-06-17
  • 2021-08-21
  • 2022-09-25
  • 2022-06-12
  • 2021-07-12
  • 2019-09-12
  • 1970-01-01
  • 2019-07-23
相关资源
最近更新 更多