云功能可将新文档从Firestore导出到GCP存储桶

时间:2020-06-27 13:53:12

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

我试图编写一个云函数,仅导出添加到“评论”子集合中的新文档。此云功能的触发器是:Cloud Firestore。但是,我的云功能部署无法通过控制台进行。有人可以帮我了解我的云功能怎么了吗?

错误消息:

Deployment failure:

Build failed: /workspace/index.js:26
    }
    ^

SyntaxError: missing ) after argument list
    at new Script (vm.js:83:7)
    at checkScriptSyntax (internal/bootstrap/node.js:620:5)
    at startup (internal/bootstrap/node.js:280:11)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3); Error ID: d984e68f

云功能代码:

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

const bucket = 'gs://bucket_name'
exports.scheduledFirestoreBackup = (event, context) => {
  const databaseName = client.databasePath(
   // process.env.GCLOUD_PROJECT,
   "fs124",
    '(default)'
  );
return client
    .exportDocuments({
      name: databaseName,
      outputUriPrefix: bucket,
      collectionIds: ['reviews'],
    })
    .onSnapshot()
    .then(snap => {
        snap.forEach(doc => {
      const response = doc.data();
      console.log(doc.data());
      return response;
    }
    
});

控制台代码段: enter image description here

1 个答案:

答案 0 :(得分:1)

您收到的消息SyntaxError: missing ) after argument list很清楚。您缺少}的右花括号)和括号then()。它应该看起来像这样:

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

const bucket = 'gs://bucket_name'
exports.scheduledFirestoreBackup = (event, context) => {
  const databaseName = client.databasePath(
   // process.env.GCLOUD_PROJECT,
   "fs124",
    '(default)'
  );
return client
    .exportDocuments({
      name: databaseName,
      outputUriPrefix: bucket,
      collectionIds: ['reviews'],
    })
    .onSnapshot()
    .then(snap => {
        snap.forEach(doc => {
      const response = doc.data();
      console.log(doc.data());
      return response;
        }); 
    });
};