首页 > 解决方案 > Firebase 模拟器错误:通道凭据必须是 ChannelCredentials 对象

问题描述

我正在使用 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 进行(我不知道它是否会产生影响)

标签: javascriptfirebasegoogle-cloud-firestoregoogle-cloud-functionsfirebase-tools

解决方案


该问题是由于 google-gax 软件包的特定版本造成的。

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

$ npm i -D google-gax


推荐阅读