android - firebase 函数中的 initializeApp() 问题

标签 android firebase npm firebase-realtime-database google-cloud-functions

我在 Firebase 云功能日志中收到警告。这些功能已正确部署并且之前可以正常工作。现在我收到一个错误。

@firebase/database: FIREBASE WARNING: Provided authentication credentials for the app named "[DEFAULT]" are invalid. This usually indicates your app was not initialized correctly. Make sure the "credential" property provided to initializeApp() is authorized to access the specified "databaseURL" and is from the correct project.

我花了 5 到 6 个小时来搜索解决方案,然后我尝试了一个解决方案,在该解决方案中我下载了 serviceAccountKey.json 文件并使用了该文件,但是在部署函数时我遇到了另一个错误。我还没有找到任何解决方案。 这是错误

Error: Error occurred while parsing your function triggers.

ReferenceError: functions is not defined
    at Object.<anonymous> (D:\Parental Sheild\Coding\defenderFunctions\functions\index.js:22:25)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)
    at C:\Users\H.A.R\AppData\Roaming\npm\node_modules\firebase-tools\lib\triggerParser.js:18:11
    at Object.<anonymous> (C:\Users\H.A.R\AppData\Roaming\npm\node_modules\firebase-tools\lib\triggerParser.js:38:3)

我已将节点升级到 8.12.0 并将 npm 升级到最新版本。

这里是初始化App的代码

const functions = require('firebase-functions');

const admin = require("firebase-admin");

const serviceAccount = require("./serviceAccountKey.json");

admin.initializeApp({
   credential: admin.credential.cert(serviceAccount),
   databaseURL: "https://database url"
});

这是我的 firebase 函数代码

exports.sendMessage = functions.database.ref('/Defender/{UserId}/Child/{PinId}/Messages/{pushId}')
    .onWrite((change,context) => {
        const message = change.after.val();
        const sender = message.from;
        const receiver = message.to;
        const userID= message.uid;
        const promises = [];

        // if (senderUid == receiverUid) {
        //     //if sender is receiver, don't send notification
        //     promises.push(change.after.ref.remove());
        //     return Promise.all(promises);
        // }

        if (userID== receiver) {

                 const getInstanceIdPromise = admin.database().ref(`/Defender/${userID}/Profile/instanceId`).once('value');
                 const getSenderUidPromise = admin.database().ref(`/Defender/${userID}/Child/${sender}/Profile/name`).once('value');

            return Promise.all([getInstanceIdPromise, getSenderUidPromise]).then(results => {
            const instanceId = results[0].val();
            const sender = results[1].val();
            console.log('notifying ' + receiver + ' about ' + message.body + ' from ' + sender);

            const payload = {
                notification: {
                    title: sender,
                    body: message.body,
                    sound: "default"
                    // icon: sender.photoURL
                }
            };

            admin.messaging().sendToDevice(instanceId, payload)
                .then(function (response) {
                    console.log("Successfully sent message:", response);
                })
                .catch(function (error) {
                    console.log("Error sending message:", error);
                });
        });

        }else{

                 const getInstanceIdPromise = admin.database().ref(`/Defender/${userID}/Child/${receiver}/Profile/instanceId`).once('value');
                 const getSenderUidPromise = admin.database().ref(`/Defender/${userID}/Profile/displayName`).once('value');

            return Promise.all([getInstanceIdPromise, getSenderUidPromise]).then(results => {
            const instanceId = results[0].val();
            const sender = results[1].val();
            console.log('notifying ' + receiver + ' about ' + message.body + ' from ' + sender);

            const payload = {
                notification: {
                    title: sender,
                    body: message.body,
                    sound: "default"
                    // icon: sender.photoURL
                }
            };

            admin.messaging().sendToDevice(instanceId, payload)
                .then(function (response) {
                    console.log("Successfully sent message:", response);
                })
                .catch(function (error) {
                    console.log("Error sending message:", error);
                });
            });

        }

    });

更新:我发现了这个问题。我正在从函数内部的数据库中读取数据并收到错误,因为我在我的 firebase 数据库上有安全规则。现在我不知道如何解决这个问题。有帮助吗?

最佳答案

您正在使用函数,但显然您从未在代码中定义过它。您可能打算在代码顶部要求 firebase-functions:

const functions = require('firebase-functions')

关于android - firebase 函数中的 initializeApp() 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52500783/

相关文章:

java - 从 Firebase 获取不进行任何更改的子项

firebase - Unity/Firebase - 项目 bundle ID 与 google-services.json 文件中的任何 bundle ID 都不匹配

node.js - JXCore打包,NPM错误

node.js - "npm install -g n"错误

node.js - npm 错误!写在结束之后。 RUN npm install 在 docker、aws 中失败

android - 如何从 ListView 中获取所选项目?

android - Android 是如何实现对 SuperNotCalledException 的检查的?

android - 异常 : mergeDebugResources in Android Studio

android - 无法在 android 中同步 gradle for firebase

javascript - angularfire2 firebase db不显示对象列表中的所有项目