Firebase Emulator Error: Channel credentials must be a ChannelCredentials object

Firebase Emulator Error: Channel credentials must be a ChannelCredentials object

我正在使用 Firebase 模拟器。直到 4 小时前,一切正常。但是现在,我模拟的云函数触发了以下错误:

>  Error with Backup TypeError: Channel credentials must be a ChannelCredentials object
>      at new ChannelImplementation (/Users/pitouli/Documents/GIT/my-app/functions/node_modules/google-gax/node_modules/@grpc/grpc-js/build/src/channel.js:67:13)
>      at new Client (/Users/pitouli/Documents/GIT/my-app/functions/node_modules/google-gax/node_modules/@grpc/grpc-js/build/src/client.js:57:36)
>      at new ServiceClientImpl (/Users/pitouli/Documents/GIT/my-app/functions/node_modules/google-gax/node_modules/@grpc/grpc-js/build/src/make-client.js:49:5)
>      at GrpcClient.createStub (/Users/pitouli/Documents/GIT/my-app/functions/node_modules/google-gax/build/src/grpc.js:220:22)

我清楚地发现了问题:每次我执行 Firestore 请求时都会出现。

例如,这是整个函数的减法:

  return new Promise((resolve, reject) => {
    db.doc(`users/${userId}`).update({ dbUpdated: new Date() })
      .then(() => {
        resolve();
      })
      .catch(e => reject(e));
  });

如果我将其替换为:

  return Promise.resolve();

我没有错误了。但显然我不再有预期的行为......

我做了一个很大的重构(我安装了airbnb风格的eslint,所以我不得不修改相当多的文件),所以我可能做错了。但是经过几个小时的研究,我还没有找到可以证明这个错误的理由:(

我在下面为您提供了我的代码的相关摘录。我的真实代码要长得多,但我单独测试了这个摘录:它重现了错误(如果我替换之前显示的 "markUserUpdated" 函数,它就会消失。)

最后但同样重要的是,我确认 Firestore 模拟器工作正常:应用程序 运行 与来自模拟器的数据运行良好。

感谢您的帮助!

index.js :

const functions = require('firebase-functions');
const { db } = require('./admin');

const DEBUG = true;

function markUserUpdated(userId) {
  return new Promise((resolve, reject) => {
    db.doc(`users/${userId}`).update({ dbUpdated: new Date() })
      .then(() => {
        if (DEBUG) console.log('User successfully marked Updated', userId);
        resolve();
      })
      .catch(e => reject(e));
  });
}

exports.writeContact = functions.firestore
  .document('users/{userId}/contacts/{contactId}')
  .onWrite((doc, context) => {
    const { userId } = context.params;
    return markUserUpdated(userId);
  });

admin.js :

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

const serviceAccount = require('../serviceAccountKey.json');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: 'https://my-app.firebaseio.com',
  storageBucket: 'my-app.appspot.com',
});

const db = admin.firestore();
const { FieldValue } = admin.firestore;
const { Timestamp } = admin.firestore;

const storage = admin.storage();

module.exports = {
  db, storage, FieldValue, Timestamp, functions,
};

编辑:代码由babel进行(不知道有没有影响)

删除functions/node_modules文件夹并重新安装它们已经解决了问题。

如果我重现导致应用程序崩溃的操作,我会回来编辑此 post。

问题是由于 google-gax 包的特定版本。

我可以通过以下步骤解决问题。

$ npm i -D google-gax

我不得不:

  1. 移除node_modules
  2. 更新依赖项
  3. 重新安装

如下

cd functions
rm -rf node_modules
npm install -g npm-check-updates
ncu -u
npm install