【问题标题】:No connection established with Cloud Functions and Firestore未与 Cloud Functions 和 Firestore 建立连接
【发布时间】:2020-04-04 14:59:25
【问题描述】:

我在从云功能连接到 Firestore 数据库时遇到问题。

我收到以下错误:

i  functions: Beginning execution of "test"
>  Error: 14 UNAVAILABLE: No connection established
>      at Object.callErrorFromStatus (/Users/michael/project/functions/node_modules/@grpc/grpc-js/build/src/call.js:30:26)
>      at Object.onReceiveStatus (/Users/michael/project/functions/node_modules/@grpc/grpc-js/build/src/client.js:174:52)
>      at Object.onReceiveStatus (/Users/michael/project/functions/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:340:141)
>      at Object.onReceiveStatus (/Users/michael/project/functions/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:303:181)
>      at Http2CallStream.outputStatus (/Users/michael/project/functions/node_modules/@grpc/grpc-js/build/src/call-stream.js:114:27)
>      at Http2CallStream.maybeOutputStatus (/Users/michael/project/functions/node_modules/@grpc/grpc-js/build/src/call-stream.js:153:22)
>      at Http2CallStream.endCall (/Users/michael/project/functions/node_modules/@grpc/grpc-js/build/src/call-stream.js:140:18)
>      at Http2CallStream.cancelWithStatus (/Users/michael/project/functions/node_modules/@grpc/grpc-js/build/src/call-stream.js:430:14)
>      at ChannelImplementation.tryPick (/Users/michael/project/functions/node_modules/@grpc/grpc-js/build/src/channel.js:214:32)
>      at Object.updateState (/Users/michael/project/functions/node_modules/@grpc/grpc-js/build/src/channel.js:82:26) {
>    code: 14,
>    details: 'No connection established',
>    metadata: Metadata { internalRepr: Map {}, options: {} }
>  }

GOOGLE_APPLICATION_CREDENTIALS 环境变量指向服务帐户凭据。

这是我的函数代码:

const functions = require("firebase-functions");
const admin = require("firebase-admin");

admin.initializeApp();
let db = admin.firestore();

exports.test = functions.https.onRequest((req, res) => {

      db.collection("users").doc("test").set({
          'name': 'Michael'
      }).then(ref => {
          res.send(ref.id);
      })
      .catch(err => {
        console.error(err)  
        res.status(500).send(err);
      })

});

...这些是我在package.json 文件中的依赖项

"dependencies": {
    "firebase-admin": "^8.10.0",
    "firebase-functions": "^3.3.0"
  }

我看到有更多人面临类似问题,他们说我应该升级到最新版本的 firebase-functions 和 firebase-admin,但就我而言,它们是最新的。我还删除了node_modules/ 并重新安装了它,但问题仍然存在。

有什么想法吗?

【问题讨论】:

  • 您不需要 googleapis 依赖项。尝试删除它。
  • 另请注意,set() 不会生成快照。调用 snap.data() 将失败。 googleapis.dev/nodejs/firestore/latest/…
  • 编辑了我的示例代码。我需要 googleapis 来做其他事情,但仍然尝试将其删除,但仍然无法正常工作。

标签: node.js firebase google-cloud-firestore google-cloud-functions


【解决方案1】:

您是否设置过名为FIRESTORE_EMULATOR_HOST 的环境变量?

如果设置了该环境变量,firebase admin SDK 将尝试连接到模拟器而不是 firestore,如果您尚未启动模拟器,则会出现Error: 14 UNAVAILABLE: No connection establishedhttps://firebase.google.com/docs/emulator-suite/connect_firestore?hl=en

【讨论】:

  • 这确实是我上周遇到的问题...; refreshenv in win 在 cmd 中不起作用;我不得不重新启动,我的脚本又开始工作了
  • 我在 .zshrc 中将 FIRESTORE_EMULATOR_HOST 设置为始终指向本地模拟器(所以我不会意外修改生产)...当我需要直接进入生产时,我忘记了这个事实...您的回答对我有帮助...谢谢。
猜你喜欢
  • 2021-09-10
  • 1970-01-01
  • 2018-10-16
  • 1970-01-01
  • 2018-03-23
  • 1970-01-01
  • 1970-01-01
  • 2017-01-26
  • 2021-02-25
相关资源
最近更新 更多