【发布时间】:2019-03-01 05:33:21
【问题描述】:
我在 Firebase 云功能日志中收到警告。这些功能已正确部署并且以前可以正常工作。现在我收到一个错误。
@firebase/database:FIREBASE 警告:为名为“[DEFAULT]”的应用提供的身份验证凭据无效。这通常表明您的应用程序未正确初始化。确保提供给 initializeApp() 的“credential”属性被授权访问指定的“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 升级到最新版本。
这里是初始化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 数据库上有安全规则。现在我不知道如何解决这个问题。有什么帮助吗?
【问题讨论】:
-
请编辑问题以显示所有相关代码。您的错误发生在此处未显示的线上。
-
@DougStevenson 我已经更新了代码。立即查看
-
我已经发布了简单的功能,并且该功能运行良好。问题在于数据库的路径。
-
我发现了这个问题。我正在从函数内部的数据库中读取数据并收到错误,因为我的 firebase 数据库上有安全规则。现在我不知道如何解决这个问题。有什么帮助吗?
标签: android firebase npm firebase-realtime-database google-cloud-functions