Pit*_*uli 4 javascript firebase firebase-tools google-cloud-functions google-cloud-firestore
我正在使用 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)
Run Code Online (Sandbox Code Playgroud)
我清楚地发现了问题:每次我执行 Firestore 请求时都会出现这个问题。
例如,这是整个函数的减法:
return new Promise((resolve, reject) => {
db.doc(`users/${userId}`).update({ dbUpdated: new Date() })
.then(() => {
resolve();
})
.catch(e => reject(e));
});
Run Code Online (Sandbox Code Playgroud)
如果我将其替换为:
return Promise.resolve();
Run Code Online (Sandbox Code Playgroud)
我不再有错误了。但显然我不再有预期的行为......
我做了一个很大的重构(我安装了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);
});
Run Code Online (Sandbox Code Playgroud)
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,
};
Run Code Online (Sandbox Code Playgroud)
编辑:代码由 Babel 处理(不知道会不会有影响)
| 归档时间: |
|
| 查看次数: |
1379 次 |
| 最近记录: |