我一直试图深入了解我用来更新Firestore中某些聚合数据的Firebase函数的问题。我建立了一个简单的测试平台,发现任何访问数据的尝试都会触发错误:
> Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
> at GoogleAuth.getApplicationDefaultAsync (/Users/michael/Documents/htdocs/vue/mjf20/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:160:19)
我试图将整个Firebase配置复制到initializeApp()函数中,但是它仍然会产生相同的错误。这是完整的测试代码:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();
exports.postCache = functions.https.onRequest((request, response) => {
console.log("Starting test function");
db.collection('myCollection').doc('myDoc').get()
.then(snap => {
if (!snap.exists) {
console.log('Document not found');
} else {
console.log(snap.data());
response.send(snap.data());
}
})
.catch(error => {
console.log('In catch block');
console.log(error);
response.send(error);
});
});
如果我取出db的东西,该功能将正常工作。即使我添加了最简单的Firestore请求,也会立即生成错误。我以前曾问过这个问题,但情况似乎有所不同,所有解决方案似乎都无效。我很困惑。