针对只允许对Firebase的CloudFunctions进行写访问和firewalld只允许指定ip访问服务器这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展android–GoogleC
针对只允许对Firebase的Cloud Functions进行写访问和firewalld只允许指定ip访问服务器这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展android – Google Cloud Function Node.js Firebase网址、Cloud Functions 数据操作问题中的 Firebase 管理员、Firebase Cloud Function 中忽略了 4GB 的内存分配、Firebase Cloud Functions - 如何从同一引用的不同子快照中获取值等相关知识,希望可以帮助到你。
本文目录一览:- 只允许对Firebase的Cloud Functions进行写访问(firewalld只允许指定ip访问服务器)
- android – Google Cloud Function Node.js Firebase网址
- Cloud Functions 数据操作问题中的 Firebase 管理员
- Firebase Cloud Function 中忽略了 4GB 的内存分配
- Firebase Cloud Functions - 如何从同一引用的不同子快照中获取值
只允许对Firebase的Cloud Functions进行写访问(firewalld只允许指定ip访问服务器)
我们如何能够通过只允许火力地堡云功能将数据写入到特定位置的规则安全数据库,以前有以UID添加到管理客户机的选择databaseAuthVariableOverride
和使用规则节UID,但现在我们通过初始化admin.initializeApp(functions.config().firebase);
,所以我不确定如何添加其他参数。
编辑 是为此发起证书的好主意吗?即
admin.initializeApp({ credential: admin.credential.cert("/path-to-cert"), databaseURL: "database-url", databaseAuthVariableOverride: { uid: "some-id" }});
admin.initializeApp(functions.config().firebase)
上面有什么好处,functions.config()
实际上是从哪里获取数据,这不只是节点模块吗?
答案1
小编典典通常,在Cloud Functions代码的顶部,您具有:
var functions = require(''firebase-functions'');
作为firebase-functions
节点模块的一部分,您可以访问,functions.config().firebase
它只是一个对象,它具有初始化Admin
SDK所需的一切,包括数据库URL和凭据实现(基于Application Default
Credentials)。如果您console.log(functions.config().firebase)
在代码中,将看到它只是具有这些属性的对象,以及可能要在代码中使用的其他一些对象。
您可以添加databaseAuthVariableOverride
到此对象以限制Admin
SDK的特权。您可以覆盖对象本身:
var firebaseConfig = functions.config().firebase;firebaseConfig.databaseAuthVariableOverride = { uid: ''some-uid'', foo: true, bar: false};admin.initializeApp(firebaseConfig);
或者,您可以使用类似的Object.assign()
方法将相关详细信息复制到新对象中:
var firebaseConfig = Object.assign({}, functions.config().firebase, { databaseAuthVariableOverride: { uid: ''some-uid'', foo: true, bar: false }});admin.initializeApp(firebaseConfig);
android – Google Cloud Function Node.js Firebase网址
这是我的节点;
const functions = require(''firebase-functions''); const admin = require(''firebase-admin''); admin.initializeApp(functions.config().firebase); exports.sendNotification = functions.https.onRequest((req,res) => { const to = req.query.to; const title = req.query.title; const body = req.query.body; var payload; if (body != undefined && body !== '''') { payload = { notification: { title: title,body: body } }; } else { payload = { notification: { title: title } }; } var options = { priority: "high",timetoLive: 60 * 60 * 24 }; if (to == ''all'') { admin.messaging().sendToTopic(to,payload,options) .then(function (response) { res.send(200,''ok''); }) .catch(function (error) { res.send(200,''Failed''); }); } else { admin.messaging().sendToDevice(to,''Failed''); }); } });
在运行推送通知时,我有URL,
https://MyProjectName.cloudfunctions.net/sendNotification?to=all&title=hello&body=EnterBodyHere
现在,请帮我安排这样的推送通知
本地主机:8080:/?schedulePush天= 17&安培;一个月= 10安培;小时= 12&安培;分钟= 22
解决方法
Cloud Functions 数据操作问题中的 Firebase 管理员
如何解决Cloud Functions 数据操作问题中的 Firebase 管理员?
Firebase 实时数据库结构
freepacks
包含两个重要元素:
-
current
,这是我将从移动应用下载的测验包 ID(从quizpacks
检索)。 -
history
,这是一个节点,我在其中添加了随时间推移放入current
中的所有测验包 ID,以及 Cloud Functions 中的预定函数。
每次执行云功能时我需要做什么
第 1 步:在 current
中添加 history
的值和当前时间戳。
第 2 步:用另一个 不在历史上的测验包 ID 替换 current
值。
完成
我是如何努力实现这个目标的
const functions = require(''firebase-functions'');
const admin = require(''firebase-admin'');
admin.initializeApp();
exports.scheduledFunction = functions.pubsub.schedule(''* * * * *'').onRun((context) => {
// Current timestamp
const dt = new Date();
const timestamp = `${
(dt.getMonth()+1).toString().padStart(2,''0'')}/${
dt.getDate().toString().padStart(2,''0'')}/${
dt.getFullYear().toString().padStart(4,''0'')} ${
dt.getHours().toString().padStart(2,''0'')}:${
dt.getMinutes().toString().padStart(2,''0'')}:${
dt.getSeconds().toString().padStart(2,''0'')}`
// Download the entire node ''freepacks''
return admin.database().ref(''quiz/freepacks'').once(''value'').then((currentSnap) => {
const currentPack = currentSnap.val().current;
// Add current to history
admin.database().ref(''quiz/freepacks/history/'' + currentPack).set(timestamp);
// Download entire history node
admin.database().ref(''quiz/freepacks/history'').once(''value'').then((historySnap) => {
const history = historySnap.val();
console.log(''HISTORY: '' + history);
// Download entire quizpacks node
admin.database().ref(''quiz/quizpacks'').once(''value'').then((quizpacksSnap) => {
for(quizpack in Object.keys(quizpacksSnap.val())) {
console.log(''IteraNDO: '' + quizpack);
// Add the new current if it isn''t in history
if (historySnap[quizpack] == undefined) {
admin.database().ref(''quiz/freepacks/current'').set(quizpack);
break;
}
}
});
})
});
});
我从之前的代码中得到了什么
起点:
第一次执行:历史更新良好但更新current
无效
第二次执行广告等等...
current
不再更新(停留在 0)
我使用 JavaScript 和 Firebase Admin 的经验是 ~0...我的代码有什么问题?在此先感谢您的帮助!
解决方法
首先是所有读/写操作都返回承诺,因此您应该处理它们。在这个答案中,我使用了 async-await
语法。 .ref("quiz/freepacks")
获取完整节点,即当前节点和历史节点,因此您不必像在原始代码中那样再次查询历史节点。其他更改只是 Javascript 调整,例如使用 .find()
而不是 for-loop
等等..
尝试将您的函数更改为:
exports.scheduledFunction = functions.pubsub
.schedule("* * * * *")
.onRun(async (context) => {
// Getting Date
const dt = new Date();
const timestamp = `${(dt.getMonth() + 1).toString().padStart(2,"0")}/${dt
.getDate()
.toString()
.padStart(2,"0")}/${dt.getFullYear().toString().padStart(4,"0")} ${dt
.getHours()
.toString()
.padStart(2,"0")}:${dt.getMinutes().toString().padStart(2,"0")}:${dt
.getSeconds()
.toString()
.padStart(2,"0")}`;
// Download the entire node ''freepacks''
const currentSnap = await firebase
.database()
.ref("quiz/freepacks")
.once("value");
// Checking current free pack ID and array of previous free packs
const currentPack = currentSnap.val().current || "default";
const freeHistoryIDs = [
...Object.keys(currentSnap.val().history || {}),currentPack,];
// Add current free pack to free history
await firebase
.database()
.ref("quiz/freepacks/history/" + currentPack)
.set(timestamp);
// Download entire quizpacks node
const quizpacksSnap = await firebase
.database()
.ref("quiz/quizpacks")
.once("value");
const quizPackIDs = Object.keys(quizpacksSnap.val() || {});
const newPack = quizPackIDs.find((id) => !freeHistoryIDs.includes(id));
console.log(newPack);
if (!newPack) {
console.log("No new pack found")
}
return firebase.database().ref("quiz/freepacks/current").set(newPack || "none");
});
Firebase Cloud Function 中忽略了 4GB 的内存分配
如何解决Firebase Cloud Function 中忽略了 4GB 的内存分配?
我想将 Firebase 云函数的内存分配限制更新为 4GB。我用 runWith
参数更新了内存值:
functions.runWith({memory: "4GB"})
然而,这被忽略了,部署的函数没有分配 4 GB 的内存。
如果我尝试使用 3GB,则会出现此错误:
错误:唯一有效的内存分配值为:128MB、256MB、512MB、1GB、2GB、4GB
所以看起来 4 GB 是一个有效值。
我做错了什么?我错过了什么吗?
如果我使用 2GB、1GB,它似乎工作得很好......它只忽略了 4GB 的值。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
Firebase Cloud Functions - 如何从同一引用的不同子快照中获取值
在您的代码中有两件事需要调整:
- 您需要遍历
user
对象,然后需要遍历每个post
对象的每个user
。有关详细信息,请参阅此 SO answer。 - 您需要通过返回 Promises chain 来正确管理 Cloud Function 的生命周期。正如您将在文档中看到的 here,关键是通过返回 JavaScript 承诺来终止执行异步处理的 Cloud Functions。在您的情况下,我们返回 Promise 链,它是一个 Promise,它由异步 Firebase 方法(
once()
和update()
)和then()
返回的 Promise 组成。为此,我们需要使用once()
方法的“promisified”版本(即once('value').then((snapshot) => {..})
),而不是您在代码中使用的回调版本。
所以以下应该可以解决问题:
exports.updateValues = functions.https.onCall((data,context) => {
const postId = data.postId; // adidas_ABC
var sum = 0;
var totalPosts = 0;
var starRating = 0;
const sneakersRef = admin.database().ref('sneakers').child(postId);
return sneakersRef // Note the return here!! => We return the Promise chain
.once('value')
.then((snapshot) => {
if (snapshot.exists()) {
const postCount = snapshot.child('postCount').val();
totalPosts = postCount;
const childSnapshot = snapshot.child('fans').val();
Object.keys(childSnapshot).forEach((user) => {
const obj = childSnapshot[user];
Object.keys(obj).forEach((e) => {
const price = obj[e].price;
sum += price;
});
});
// I let you dealing with the calculations to get starRating
return sneakersRef.update({ sum: sum,starRating: starRating });
} else {
console.log("doesn't exist");
return null; // Note the return here
}
})
.catch((error) => {
console.log('something went wrong: ',error);
return null; // Note the return here
});
});
关于只允许对Firebase的Cloud Functions进行写访问和firewalld只允许指定ip访问服务器的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于android – Google Cloud Function Node.js Firebase网址、Cloud Functions 数据操作问题中的 Firebase 管理员、Firebase Cloud Function 中忽略了 4GB 的内存分配、Firebase Cloud Functions - 如何从同一引用的不同子快照中获取值的相关信息,请在本站寻找。
本文标签: