云功能模拟器无法读取或写入Firestore数据库

时间:2019-06-14 17:46:54

标签: javascript firebase google-cloud-firestore google-cloud-functions firebase-cli

我昨天在模拟器上本地运行云功能,没有问题。重新启动后返回并设置export GOOGLE_APPLICATION_CREDENTIALS=时,我现在无法读取或写入数据库。我删除了密钥,创建了一个新密钥,但仍然遇到相同的问题。我创建了一个简单的函数,该函数运行时没有错误,但是在模拟器中运行时不会读取或写入数据库。 .get()函数表示找不到任何内容,.set()不返回任何内容,也没有写入数据库。

编辑*我的函数在部署到Firebase时确实可以运行,但是我有一个函数可以解析并加载大量数据,由于需要花费多长时间,这些数据我不得不在本地运行。

exports.dumbFunction = functions.https.onRequest((request, response) => {
  admin
    .firestore()
    .collection("test")
    .doc("hello")
    .get()
    .then(function(doc) {
      if (doc.exists) {
        console.log("You got me" + doc.data());
      } else {
        console.log("I couldn't find anything");
      }
    });
  admin
    .firestore()
    .collection("test")
    .doc("test123")
    .set({
      setme: "ok",
      asdf: "asdf"
    })
    .catch(err => {
      console.log("did I error?");
      console.log(err);
    });
  console.log("hi again");

});

我已按照以下说明进行操作。

https://cloud.google.com/docs/authentication/getting-started

https://firebase.google.com/docs/functions/local-emulator

https://cloud.google.com/storage/docs/reference/libraries

当我在第三个链接中运行node.js示例时,我收到以下错误消息,我认为这是我的问题,但我不知道为什么或如何解决此问题。

Unknown network resource requested!
   - URL: "http://metadata.google.internal./computeMetadata/v1/instance"
  ERROR: Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
     at GoogleAuth.<anonymous> (/home/ben/Development/polity/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:168:23)
     at Generator.next (<anonymous>)
     at fulfilled (/home/ben/Development/polity/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:19:58)
     at processTicksAndRejections (internal/process/task_queues.js:89:5)

当我echo $GOOGLE_APPLICATION_CREDENTIALS表示它已设置为包含密钥的JSON文件时。

我现在在其他功能上也看到了Unknown network resource requested!,这是我在工作时没有收到的。

有什么主意吗?

非常感谢

1 个答案:

答案 0 :(得分:0)

仍然无法解决此问题,但我找到了解决方法。将功能框架用于nodejs可以正常工作。

https://github.com/GoogleCloudPlatform/functions-framework-nodejs