gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 23:32:57 28 4
gpt4 key购买 nike

我在 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com