【问题标题】:subcollection not getting exported through Firestore export cloud function子集合未通过 Firestore 导出云功能导出
【发布时间】:2020-10-26 03:34:48
【问题描述】:

以下是我将 Firestore 导出到 GCP 存储桶的云功能。我试图通过 'collectionsToBackup' const 传递 CollectionIDs 名称,我通过将 current_date 附加到名称来创建该名称,因为我的子集合名称的格式为:sub_collection_name_yyyymmdd。然而,该函数在执行时部署,它不会导出我的子集合:那一天的 most_valuable_items_yyyymmdd。有人可以帮我确定我的功能有什么问题吗?

代码:

const firestore = require('@google-cloud/firestore');
const client = new firestore.v1.FirestoreAdminClient();

const bucket = 'gs://BUCKET_NAME'
const date_ob = new Date();
const date = ("0" + date_ob.getDate()).slice(-2);
const month = ("0" + (date_ob.getMonth() + 1)).slice(-2);
const year = date_ob.getFullYear();
const new_date= (year + month + date);

const collectionsToBackup= ("'" + "most_valuable_items" + "_" + new_date + "'");


exports.scheduledFirestoreBackup = (event, context) => {
  const databaseName = client.databasePath(
   // process.env.GCLOUD_PROJECT,
   "f-2",
    '(default)'
  );



return client
    .exportDocuments({
      name: databaseName,
      outputUriPrefix: bucket,
      collectionIds: [collectionsToBackup]
    })
    .then(responses => {
      const response = responses[0];
      console.log(`Operation Name: ${response['name']}`);

      return response;
    })
    .catch(err => {
      console.error(err);
    });
};

【问题讨论】:

    标签: javascript google-cloud-platform google-cloud-firestore google-cloud-functions


    【解决方案1】:

    您的问题似乎与使用 Firestore 查询和返回信息有关。正如社区 here 的另一个类似案例中所解释的,您需要考虑 Firestore 中查询的限制。

    此限制与允许您查询子集合的可能性和功能有关,因此它们被发送到您的存储桶。在 Firestore 中查询是一个浅层的过程,不幸的是,您不能一次返回整个集合及其自己的子集合。您需要读取每个子集合,以便您拥有它的数据以及父集合。

    因此,总而言之,这与您的代码错误无关,而是与更改您读取数据的方式(集合和子集合)有关,因此您加载数据,然后将其发送到存储桶。这个答案here 阐明了更多的可能性。

    如果信息澄清了您的疑问,请告诉我!

    【讨论】:

    • @gso_gabriel :我的收藏名称是确定的。对于今天的数据,它的 'most_valuable_items_20200706'。明天我的出口操作将是“most_valuable_items_20200707”。如果我在我的 collectionID 中明确提及 sub_collection 名称,则导出工作正常,但如果我尝试通过 const: collectionsToBackup 传递它,它就不起作用。 collectionIds: ['most_valuable_items_20200706'],工作得很好。但是我需要通过一些变量来传递 collectionIds 的值,比如 collectionsToBackup(在这种情况下),因为我的 sub_collection 名称的日期部分每天都在变化。我该怎么做?跨度>
    猜你喜欢
    • 2021-05-13
    • 1970-01-01
    • 2019-05-06
    • 2019-11-02
    • 1970-01-01
    • 2020-12-09
    • 2019-12-22
    • 2020-05-08
    • 1970-01-01
    相关资源
    最近更新 更多