Google cloud firestore firebase函数上的firebase admin无法正确初始化EAPP

Google cloud firestore firebase函数上的firebase admin无法正确初始化EAPP,google-cloud-firestore,firebase-authentication,google-cloud-functions,firebase-admin,firebase-cli,Google Cloud Firestore,Firebase Authentication,Google Cloud Functions,Firebase Admin,Firebase Cli,我试图使用firebase云函数和firebase admin组合来创建一个路由,以便在auth中向我的用户添加auth声明。 我用firebase serve进行了测试,并尝试了以下代码: const functions = require('firebase-functions'); const admin = require("firebase-admin"); admin.initializeApp(functions.config().firebase); const func =

我试图使用firebase云函数和firebase admin组合来创建一个路由,以便在auth中向我的用户添加auth声明。 我用firebase serve进行了测试,并尝试了以下代码:

const functions = require('firebase-functions');
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);

const func = () => {
    admin.auth().setCustomUserClaims("(req.params.uid", {isActivated: true}).then(() => {
        console.log("Done")
        })
        .catch(e => console.error(e))
}
setTimeout(func, 4000)


// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
exports.api = functions.https.onRequest((request, response) => {
console.log(functions.config())
 response.send("Hello from Firebase!");
});
这实际上不起作用,给了我以下的错误:

{ Error: Credential implementation provided to initializeApp() via the "credential" property failed to fetch a valid Google OAuth2 access token with the following error: "Error fetching access token: Error while making request: getaddrinfo ENOTFOUND metadata.google.internal metadata.google.internal:80. Error code: ENOTFOUND".
>      at FirebaseAppError.FirebaseError [as constructor] (C:\Users\jishn\WorkSpace\Works\Status\firebase\functions\node_modules\firebase-admin\lib\utils\error.js:42:28)
>      at FirebaseAppError.PrefixedFirebaseError [as constructor] (C:\Users\jishn\WorkSpace\Works\Status\firebase\functions\node_modules\firebase-admin\lib\utils\error.js:88:28)
>      at new FirebaseAppError (C:\Users\jishn\WorkSpace\Works\Status\firebase\functions\node_modules\firebase-admin\lib\utils\error.js:123:28)
>      at C:\Users\jishn\WorkSpace\Works\Status\firebase\functions\node_modules\firebase-admin\lib\firebase-app.js:121:23
>      at process._tickCallback (internal/process/next_tick.js:68:7)
>    errorInfo:
>     { code: 'app/invalid-credential',
>       message:
>        'Credential implementation provided to initializeApp() via the "credential" property failed to fetch a valid Google OAuth2 access token with the following 
error: "Error fetching access token: Error while making request: getaddrinfo ENOTFOUND metadata.google.internal metadata.google.internal:80. Error code: ENOTFOUND".' },
>    codePrefix: 'app' }
一篇博文说,如果我想在firebase admin中使用firestore和实时数据库以外的任何东西,我必须进行一些额外的配置,因此我尝试使用以下代码测试firestore是否工作正常:

// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');

// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp();

exports.api = functions.https.onRequest((req, res) => {
    var db = admin.firestore();
    db.collection('users').get()
    .then(snapshot => {
        snapshot.forEach(doc => {
            console.log(doc.id, '=>', doc.data());
        });
    })
    .catch(err => {
        console.log('Error getting documents', err);
    }); 
  res.send("Hi")
});
它再次给了我另一个与管理员凭据本身相关的错误:

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 (C:\Users\jishn\WorkSpace\Works\Status\firebase\functions\node_modules\google-auth-library\build\src\auth\googleauth.js:159:19)
>      at process._tickCallback (internal/process/next_tick.js:68:7)
functions.config()
返回
{}

无论发生了什么,都会发生在火力基地发球命令上。 下面的代码使用serviceAccount.json文件:

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://status-kgi.firebaseio.com"
});

但我认为,必须对云功能使用这种管理初始化不是一个好主意。

来自Github问题解决方案:

通过查看一些旧问题,我相信这是由#2214在8.5.0版中修复的

解决办法如下:

const admin=require('firebase-admin');
admin.initializeApp();//函数.config().firebase不需要

来自Github的问题解决方案:

通过查看一些旧问题,我相信这是由#2214在8.5.0版中修复的

解决办法如下:

const admin=require('firebase-admin');
admin.initializeApp();//函数.config().firebase不需要

请编辑问题,使其非常具体地说明您是如何获得此错误的。您是否在firebase emulator中本地运行?@DougStevenson我在laptopJust上运行firebase Service运行*firebase init选择了云函数和主机,然后将上述代码添加到index.js文件中,并将.api函数添加到firebase.json这是一个常见问题。如果您搜索该错误消息,您将发现大量信息。如果您没有看到解决问题的问题,请使用firebase工具提交问题@DougStevenson已经尝试了很多搜索,但是没有找到针对具体案例的结果。这个文件中根本没有庞大的代码。只有模块导入、管理初始化和http触发器。就这些。我希望我的小测试代码中没有任何错误。请编辑问题,使其非常具体地说明您是如何获得此错误的。您是否在firebase emulator中本地运行?@DougStevenson我在laptopJust上运行firebase Service运行*firebase init选择了云函数和主机,然后将上述代码添加到index.js文件中,并将.api函数添加到firebase.json这是一个常见问题。如果您搜索该错误消息,您将发现大量信息。如果您没有看到解决问题的问题,请使用firebase工具提交问题@DougStevenson已经尝试了很多搜索,但是没有找到针对具体案例的结果。这个文件中根本没有庞大的代码。只有模块导入、管理初始化和http触发器。就这些。我希望我的小测试代码没有任何错误。