Javascript Firebase HTTP Cloud Functions - 读取数据库一次
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43913139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Firebase HTTP Cloud Functions - Read database once
提问by SteveEdson
I have a Firebase HTTPs function. The function needs to read a value from a Firebase database based on the query parameter, and return a result based on this data.
我有一个 Firebase HTTPs 功能。该函数需要根据查询参数从 Firebase 数据库中读取一个值,并根据该数据返回一个结果。
The Firebase JS SDK says to do this using:
Firebase JS SDK 说要使用以下方法执行此操作:
return firebase.database().ref('/users/' + userId).once('value').then(function(snapshot) {
var username = snapshot.val().username;
// ...
});
However, the Cloud functions examples have:
但是,云函数示例具有:
var functions = require('firebase-functions');
functions.database.ref('/');
But the DB reference doesn't have the method once, only onWrite(https://firebase.google.com/docs/reference/functions/functions.database.RefBuilder). This is obviously for DB write functions, rather than HTTP functions.
但是数据库参考没有方法once,只有onWrite(https://firebase.google.com/docs/reference/functions/functions.database.RefBuilder)。这显然是针对 DB 写函数,而不是 HTTP 函数。
Is there a correct way to read from the database once in a HTTP function? Can I use the normal Firebase SDK, or is there a better way?
在 HTTP 函数中是否有正确的方法从数据库中读取一次?我可以使用普通的 Firebase SDK,还是有更好的方法?
Thanks.
谢谢。
回答by MGR Programming
I found the solution in combining the answer here on how to get the parameter and an answer from Michael Blight to How to run query from inside of Cloud function?
我找到了解决方案,将此处关于如何获取参数的答案和 Michael Blight 的答案结合到 如何从 Cloud 函数内部运行查询?
The answer there also shows what is required to use firebase-admin.
那里的答案还显示了使用 firebase-admin 所需的条件。
The following works for me when calling my-project.firebaseapp.com/event/123/.
调用 my-project.firebaseapp.com/event/123/ 时,以下内容对我有用。
var functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.showEvent = functions.https.onRequest((req, res) => {
const params = req.url.split("/");
const eventId = params[2];
return admin.database().ref('events/' + eventId).once('value', (snapshot) => {
var event = snapshot.val();
res.send(`
<!doctype html>
<html>
<head>
<title>${event.name}</title>
</head>
<body>
<h1>Title ${event. name} in ${event.city}</h1>
</body>
</html>`
);
});
});
回答by Frank van Puffelen
You're confusing two parts:
你混淆了两部分:
- the
firebase-functionsmodule, which contains the logic to triggerbased on database calls withfunctions.database.ref('/path').onWrite(). - the
firebase-adminmodule, which allows your function to call intothe database.
- 的
firebase-functions模块,其中包含的逻辑来触发基于与数据库调用functions.database.ref('/path').onWrite()。 - 该
firebase-admin模块,它可以让你的函数调用到数据库中。
Since you have a HTTP function, you should trigger as the documentation for HTTP functions shows:
由于您有一个 HTTP 函数,您应该触发,如HTTP 函数的文档所示:
exports.data = functions.https.onRequest((req, res) => {
// ...
});
Then in your function, you access the database as the documentation for the Admin SDK shows:
然后在您的函数中,您访问数据库,如Admin SDK 文档所示:
return admin.database().ref('/users/' + userId).once('value').then(function(snapshot) {
var username = snapshot.val().username;
// ...
});
So in total:
所以总的来说:
exports.date = functions.https.onRequest((req, res) => {
admin.database().ref('/users/' + userId).once('value').then(function(snapshot) {
var username = snapshot.val().username;
res.status(200).send(username);
});
});
Note that this is a tricky pattern. The call to the database happens asynchronously and may take some time to complete. While waiting for that, the HTTP function may time out and be terminated by the Google Cloud Functions system. See this section of the documentation.
请注意,这是一个棘手的模式。对数据库的调用是异步发生的,可能需要一些时间才能完成。在等待期间,HTTP 函数可能会超时并被 Google Cloud Functions 系统终止。请参阅文档的这一部分。
As a general rule I'd recommend using a Firebase Database SDK or its REST API to access the database and not rely on a HTTP function as middleware.
作为一般规则,我建议使用 Firebase 数据库 SDK 或其 REST API 来访问数据库,而不是依赖 HTTP 函数作为中间件。

