简体   繁体   English

Firebase Function 执行耗时 2794 毫秒,完成状态为:“确定”,...但 Firestore 中没有发生任何变化

[英]Firebase Function execution took 2794 ms, finished with status: 'ok' ,... but no change took place in Firestore

I am trying to update all the documents in a nested subcollection in Firestore using Cloud Functions with NodeJs.我正在尝试使用 Cloud Functions 和 NodeJs 更新 Firestore 中嵌套子集合中的所有文档。

collection().doc().collection.doc().collection().doc() collection().doc().collection.doc().collection().doc()

/timeline/105358539742688769892/publisherId/105358539742688769892/posts/29b975f2-0d53-4e06-8365-3d5442355a78 /timeline/105358539742688769892/publisherId/105358539742688769892/posts/29b975f2-0d53-4e06-8365-3d5442355a78 在此处输入图像描述

This is my code.这是我的代码。

exports.onCreateTimelineTest = functions.firestore
  .document("/timelineTest/{docId}")
  .onCreate(async (snapshot, context) => {

const timeNow =new Date();
const db = admin.firestore();
    return db.collection("timeline")
    .get()
    .then((timelineQuerySnapshot) => {
     timelineQuerySnapshot.forEach(async(timelineQuerySnapshot) => {


      console.log("all the documnets inside timeline are", timelineQuerySnapshot.id);

       await db.collection("timeline")
        .doc(timelineQuerySnapshot.id)
        .collection("publisherId")
        .get()
        .then((publisherSnapshot)=>{
    publisherSnapshot.forEach(async(publisherSnapshot)=>{


      console.log("all the documnets inside timeline are", publisherSnapshot.id);

    await db.collection("timeline")
     .doc(timelineQuerySnapshot.id)
     .collection("publisherId")
     .doc(publisherSnapshot.id)
     .collection("posts")
     .where('uploadTime', '<=', timeNow)
     .get()
     .then((postSnap)=>{
    postSnap.forEach(async(postSnap)=>{


    console.log("all the documnets inside timeline are", postSnap.id);

    return db.collection("timeline")
    .doc(timelineQuerySnapshot.id)
    .collection("publisherId")
    .doc(publisherSnapshot.id)
    .collection("posts")
    .doc(postSnap.id)
    .set({
    'name': 'madhav'
                         }).catch((err)=>{
                         console.log('Error getting documents', err);
                         return Promise.reject(err);
                         });
                      });
        return null
        }).catch((err) => {
                   console.log('Error getting documents', err);
                   return Promise.reject(err);
                  });
        });
        return null
        }).catch((err) => {
         console.log('Error getting documents', err);
         return Promise.reject(err);
        });
    });
    return null
    }).catch((err) => {
     console.log('Error getting documents', err);
     return Promise.reject(err);
    });

  });

The functions works fine in emulator, and after deploying, it finished with status 'ok'.这些功能在模拟器中运行良好,部署后,状态为“ok”。 But when I check the firestore, I see that there is no change taken place.但是当我检查 firestore 时,我发现没有发生任何变化。 It is still the same.它仍然是一样的。

在此处输入图像描述

After spending 2 days on it, I found the solution and it was extremely dumb.花了 2 天时间后,我找到了解决方案,而且非常愚蠢。

Here in the screenshot it says that,在屏幕截图中,它说,

this document does not exist, it will not appear in queries and snapshots.该文档不存在,它不会出现在查询和快照中。

So, I had to add a field to each of the document and then the functions started working fine.因此,我必须向每个文档添加一个字段,然后函数才能正常工作。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM