我正在尝试为Firebase Cloud Functions编写onCall函数,以对firestore数据库执行高级查询任务(例如将文本查询与AutoML自然语言进行比对以获取类别等),但我在尝试从函数查询数据库时遇到了问题:
Error getting documents :: Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
at GoogleAuth.getApplicationDefaultAsync (/srv/node_modules/google-auth-library/build/src/auth/googleauth.js:161:19)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:229:7)
功能:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();
exports.query = functions.https.onCall((data, context) => {
const text = data.text;
var results = [];
const promise = db.collection('providers').get()
promise.then((snapshot) => {
console.log('marker');
snapshot.forEach((doc) => {
results.push({id: doc.id, data: doc.data()});
});
console.log('yessir');
return {results: results};
}).catch((err) => {
console.log('Error getting documents :: ', err)
console.log('nosir');
return {results: "no results"};
});
});
更长的输出:
Function execution started
Function execution took 8ms, finished with status code: 200
Error getting documents :: (etc, same error)
nosir
例2(运行中未改变):
Function execution started
Function execution took 1200 ms, finished with status code: 200
marker
yessir
我想不出这个问题从何而来,也不知道如何解决。有人能帮忙吗?
谢谢。
admin.initializeApp({ credential:admin.credential.applicationDefault() });
- Pablo Almécija Rodríguezfirebase login
命令,问题就解决了。 - Cristhian Fernández