Firebase HTTP云端功能 - 读取数据库一次 [英] Firebase HTTP Cloud Functions - Read database once

查看:188
本文介绍了Firebase HTTP云端功能 - 读取数据库一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Firebase HTTPs功能。该功能需要根据查询参数从Firebase数据库读取值,并根据此数据返回结果。



Firebase JS SDK说要使用:

 返回firebase.database()。ref('/ users /'+ userId).once('value')。然后(函数(快照){
var username = snapshot.val()。username;
// ...
});

然而,Cloud函数的例子有:

  var functions = require('firebase-functions'); 

functions.database.ref('/');

但是DB引用没有方法一次,只有 onWrite https://firebase.google.com/docs/reference/functions/functions.database.RefBuilder )。这显然是数据库写入函数,而不是HTTP函数。

在HTTP函数中是否有正确的方式从数据库读取数据?我可以使用正常的Firebase SDK,还是有更好的方法?



谢谢。

解决方案

我找到了解决方案,在这里结合答案如何获取参数和从迈克尔Blight的答案
如何从云端函数内执行查询?



这里的答案也显示使用firebase-admin需要什么。



以下方法适用于我在调用my-project.firebaseapp.com/event/123/时的情况。

  var functions = require('firebase-functions'); 
const admin = require('firebase-admin');

admin.initializeApp(functions.config()。firebase);
$ b exports.showEvent = functions.https.onRequest((req,res)=> {
const params = req.url.split(/);
const eventId = params [2];
返回admin.database()。ref('events /'+ eventId).once('value',(snapshot)=> {
var event = snapshot。 val();
res.send(
<!doctype html>
< html>
< head>
< title> $ {event在$ {event.city}中标题$ {event.name}< /name>< / title>
< / head>
< body>
< / h1& h1>
< / body>
< / html>`
);
});
});


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.

The Firebase JS SDK says to do this using:

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.

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?

Thanks.

解决方案

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?

The answer there also shows what is required to use firebase-admin.

The following works for me when calling 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>`
        );
     });
});

这篇关于Firebase HTTP云端功能 - 读取数据库一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆