警告,缺少FIREBASE_CONFIG和GCLOUD_PROJECT环境变量.初始化firebase-admin将失败 [英] Warning, FIREBASE_CONFIG and GCLOUD_PROJECT environment variables are missing. Initializing firebase-admin will fail
问题描述
我正在使用本教程来设置Firebase管理SDK.
I am using this tutorial, to setup firebase admin SDK.
https://firebase.google.com/docs/admin/setup
我从Firebase控制台下载了一个json文件(服务帐户). 它位于路径下:
I have downloaed a json file (service account) from firebase console. It is under the the path:
C:\ct\functions\src\cargo-tender-firebase-adminsdk-8e307-c6b82762d2.json
我已经设置了一个环境变量:
I have set an environment variable:
GOOGLE_APPLICATION_CREDENTIALS=C:\ct\functions\src\cargo-tender-firebase-adminsdk-8e307-c6b82762d2.json
当我运行脚本时,收到以下警告:
And when I run the script, I receive the following Warning:
Warning, FIREBASE_CONFIG and GCLOUD_PROJECT environment variables are missing. Initializing firebase-admin will fail
为什么?以及如何解决这个问题?如何测试代码? (我想在onCreate事件中发送推送通知)
我的代码:
"use strict";
exports.__esModule = true;
var functions = require("firebase-functions");
var admin = require("firebase-admin");
admin.initializeApp({
credential: admin.credential.applicationDefault(),
databaseURL: 'https://cargo-tender.firebaseio.com'
});
exports.sendPushNotification = functions.database
.ref('/user-chat')
.onCreate(function (event) {
var payload = {
notification: {
title: 'Title',
body: 'come check it',
badge: '0',
sound: 'default'
}
};
return admin
.database()
.ref('fcmToken')
.once('value')
.then(function (allToken) {
if (allToken.val()) {
var token = Object.keys(allToken.val());
return admin
.messaging()
.sendToDevice(token, payload)
.then(function (response) {
//
});
}
});
});
版本:
PS C:\ct\functions> tsc -v
Version 3.5.2
PS C:\ct\functions> firebase -V
7.0.2
PS C:\ct\functions> node -v
v10.16.0
PS C:\ct\functions> npm -v
6.9.0
推荐答案
如我所读-我应该安装一个模拟器以尝试在本地工作.
另外,firebase控制台上还有一个功能页面.在哪里可以测试该功能并查看console.log()
输出.
As I read - I should install an emulator to try to work locally.
Also, there is a functions page on the firebase console. Where I can test the function and see console.log()
outputs.
这篇关于警告,缺少FIREBASE_CONFIG和GCLOUD_PROJECT环境变量.初始化firebase-admin将失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!