错误:无法加载默认凭据(Firebase 函数到 firestore) [英] Error: Could not load the default credentials (Firebase function to firestore)

查看:41
本文介绍了错误:无法加载默认凭据(Firebase 函数到 firestore)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 Firebase Cloud Functions 编写一个 onCall 函数,该函数在 Firestore 数据库上执行高级查询任务(即根据 AutoML 自然语言检查文本查询以获取类别等),但我一直在尝试时遇到问题从函数查询数据库:

I am attempting to write an onCall function for Firebase Cloud Functions that performs advanced querying tasks on a firestore database (i.e. checking a text query up against AutoML natural lang to get a category, etc) but I keep running into a problem trying to query the database from the function:

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(运行中没有变化):

Example 2 (no change in running):

Function execution started
Function execution took 1200 ms, finished with status code: 200
marker
yessir

我不知道这个问题是从哪里来的,也不知道如何解决.有什么帮助吗?

I can't figure out where this problem is coming from or how to resolve it. Any help?

问候.

推荐答案

我遇到了同样的错误无法加载默认凭据".

I had the same error "Could not load the default credentials".

使用 npm update 更新我的项目依赖项后发生错误.更准确地说,firebase-adminfirebase-functions.

The error occured after updating my project dependencies with npm update. More precisely firebase-admin and firebase-functions.

更新前:

"dependencies": {
    "@google-cloud/firestore": "^1.3.0",
    "firebase-admin": "~7.0.0",
    "firebase-functions": "^2.2.0"
}

更新后:

"dependencies": {
    "@google-cloud/firestore": "^1.3.0",
    "firebase-admin": "^8.6.0",
    "firebase-functions": "^3.3.0"
}

我将 serviceAccountKey.json 添加到我的项目中,并使用我的 firebase 项目的服务帐户设置中提供的代码更改了导入.

I added the serviceAccountKey.json to my project and changed the imports with the code provided at the service account setting of my firebase project.

来自:

var admin = require('firebase-admin')

admin.initializeApp()

致:

var admin = require('firebase-admin');    
var serviceAccount = require('path/to/serviceAccountKey.json');

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

请参阅下面的 @Fernando Rocha 的回答以访问您的 Firebase 项目的帐户设置.

See @Fernando Rocha's answer below to access the account setting of your firebase project.

这篇关于错误:无法加载默认凭据(Firebase 函数到 firestore)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆