Warning, FIREBASE_CONFIG and GCLOUD_PROJECT enviro

2019-08-19 01:17发布

问题:

I am using this tutorial, to setup firebase admin SDK.

https://firebase.google.com/docs/admin/setup

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

Why? And how to fix this problem? How can I test the code? (I want to send Push notification in onCreate event)

My code:

"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) {
                    // 
                });
        }
    });
});

Versions:

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

回答1:

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.