我在Firebase云功能日志中收到警告。这些功能已正确部署并且之前可以正常工作。现在我得到一个错误。
@ firebase /数据库:防火墙警告:为名为“ [DEFAULT]”的应用程序提供的身份验证凭据无效。这通常表明您的应用未正确初始化。确保提供给initializeApp()的“凭据”属性被授权访问指定的“ databaseURL”并且来自正确的项目。
我花了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升级到最新版本。
此处是初始化应用的代码
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数据库具有安全规则。现在我不知道如何解决这个问题。有帮助吗?
答案 0 :(得分:1)
您正在使用functions
,但显然您从未在代码中定义它。您可能打算在代码顶部要求firebase-functions
:
const functions = require('firebase-functions')