首页 > 解决方案 > 如何更新 Firestore 数据库中的值

问题描述

我正在尝试更新 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,
    });
};

标签: javascriptfirebasereact-nativegoogle-cloud-firestore

解决方案


您需要知道文档 ID(或对其进行引用)才能更新它。尝试以下功能:

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

推荐阅读