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

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

昨天我使用模拟器在本地运行我的云功能没有问题。当我回来并设置我的 export GOOGLE_APPLICATION_CREDENTIALS= 重启后,我现在无法读取或写入数据库。我删除了我的 key ,创建了一个新 key ,但我仍然遇到同样的问题。我创建了一个简单的函数,它运行时没有错误,但在模拟器中运行时它不会读取或写入数据库。 .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)

当我回显 $GOOGLE_APPLICATION_CREDENTIALS 时,它显示它已设置为包含我的 key 的 JSON 文件。

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

有什么想法吗?

非常感谢

最佳答案

仍然无法正常工作,但我找到了解决方法。使用 nodejs 的函数框架工作得很好。

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

关于javascript - 云功能模拟器无法读取或写入 firestore 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56602985/

相关文章:

javascript - 如何查找给定数组中重复数字的计数并将输出作为对象返回

php - Google Firebase 通知无法订阅主题

google-cloud-firestore - Firestore : import sql database

javascript - 帮助解决错误 SCRIPT5007 : Unable to set value of the property 'onclick'

javascript - Angular 2 : importing router bundle

javascript - 使用 UIAElementArray 中 Number 的 withName 获取元素

javascript - 数据库写入时未触发云函数

ios - 当需要根据特定键过滤用户帖子时如何构建(保存/检索)?

javascript - Angular Firestore 根据返回的 bool 值显示一个按钮,该 bool 值检查 Firestore 文档是否存在

javascript - Firestore 查询返回空快照