【问题标题】:Property 'id' does not exist on type 'QuerySnapshot<DocumentData>'类型 'QuerySnapshot<DocumentData>' 上不存在属性 'id'
【发布时间】:2021-10-14 20:00:51
【问题描述】:

我在以下代码中收到此错误:“QuerySnapshot”类型上不存在属性“id”。

我的代码出错了。我试图首先从 Firestore 查询用户列表(在集合中),而不是使用数据从其他集合中查询。我在更新列表时似乎出错了,这可能是由于查询部分的错误引起的。

const dailyTaskFunction = functions.pubsub.schedule("0 0 * * *").timeZone("GMT +3").onRun(async () => { 
        
    const users = await db.collection("users").where("location", "==", "GMT+3").get();
    const taskPromises = users.docs.map((doc) => {
        return db
          .collection("tasks")
          .where("dailyTasks", "==", true)
          .where("completed", "==", true)
          .where("userID", "==", doc.get("userID"))
          .get();
    });
    const taskDocs = await Promise.all(taskPromises);
    const actionsPromises = taskDocs.map((snapshot) => {
        return db.collection("tasks").doc(snapshot.id).set({ completed: false }, { merge: true });
    });
    await Promise.all(actionsPromises);
    return null;
});

【问题讨论】:

    标签: node.js typescript google-cloud-firestore google-cloud-functions


    【解决方案1】:

    QuerySnapshot 类,不包含任何 id 属性。但是,QueryDocumentSnapshot 可以。根据文档,id 是字符串类型并提供文档的 ID。这意味着您需要使用forEach 循环遍历snapshot 对象以获取其类型为QueryDocumentSnapshot 的子对象。现在,您可以简单地为每个文档使用 id 属性,并在您的参考文献中进一步使用它。

    【讨论】:

    • 如果你能分享代码真的很有帮助
    • 您应该根据我回答中的信息自行尝试,如果出现其他问题,请提出另一个问题。
    • 如果您认为我的回答对未来的 Firebase 开发者也有帮助,也请考虑投上一票(▲)。我真的很感激。谢谢!
    【解决方案2】:

    你需要遍历返回给你的变量taskDocs的查询快照,这样你就可以得到每个项目的QueryDocumentSnapshot,然后你就可以得到id。

    const dailyTaskFunction = functions.pubsub
      .schedule('0 0 * * *')
      .timeZone('GMT +3')
      .onRun(async () => {
        const users = await db
          .collection('users')
          .where('location', '==', 'GMT+3')
          .get()
        const taskPromises = users.docs.map((doc) => {
          return db
            .collection('tasks')
            .where('dailyTasks', '==', true)
            .where('completed', '==', true)
            .where('userID', '==', doc.get('userID'))
            .get()
        })
        const taskDocs = await Promise.all(taskPromises)
        const actionsPromises = taskDocs.map((snapshots) => {
          return snapshots.docs.map((snapshot) => {
            return db
              .collection('tasks')
              .doc(snapshot.id)
              .set({ completed: false }, { merge: true })
          })
        })
        await Promise.all(actionsPromises)
        return null
      })
    

    【讨论】:

    • 我收到此错误:“QuerySnapshot[]”类型上不存在属性“docs”
    • 编辑反映
    • 没有错误但代码不工作
    • 您需要提供更多详细信息,什么不起作用?
    • 在 VSCode 中没有错误,但是当我运行代码时,它没有更新它应该更新的字段。
    【解决方案3】:

    taskDocs 是一个 QuerySnapshot 数组。您需要在其中的每个 QuerySnapshot 中对每个文档运行循环。这些 DocumentSnapshots 将具有文档 ID:

        const actionsPromises = []
        
        taskDocs.forEach((userTasksDocs) => {
          userTasksDocs.docs.forEach((taskDoc) => {
            actionsPromises.push(
              db
                .collection('tasks')
                .doc(taskDoc.id)
                .set({ completed: false }, { merge: true })
            );
          });
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-15
      • 2023-01-19
      • 1970-01-01
      • 1970-01-01
      • 2022-08-18
      • 2018-10-05
      • 2021-11-24
      相关资源
      最近更新 更多