Firebase Cloud功能:如何在http请求中使用通配符推送数据 [英] Firebase Cloud Functions: How to push data using wildcards in http requests

查看:98
本文介绍了Firebase Cloud功能:如何在http请求中使用通配符推送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在探索Firebase中的新Cloud Functions功能,因此我知道通配符如何在Realtime数据库触发器中工作,但是当我尝试在http请求中添加通配符时,它将忽略通配符并将新数据推入新的 {userId} 对象.

I'm exploring new Cloud Functions feature in Firebase, so I know how wildcards work in Realtime database triggers, but when I'm trying to add wildcards in http request, it ignores wildcards and pushes new data into the new {userId} object.

基本上,我正在尝试创建一个函数,该函数将从'today'对象中的所有用户那里获取数据并将其推送到'yesterday'对象中(类似于统计信息或过程).为此,我正在使用本主题基于cron示例.

Basically, I'm trying to make a function, which will take data from all the users in 'today' object and push it into 'yesterday' object (something like statistics or process). For that I'm using this topic based on cron example.

我已经开始构建由http请求触发的简单功能,其想法是为所有用户推送一些数据( {userId} 通配符),但是数据被推送到新的 {userId} 对象,忽略通配符.

I have started with building simple function triggered by http request, with an idea of pushing some data for all the users ({userId} wildcard), but the data is being pushed into new {userId} object, ignoring the wildcard.

exports.addEntry = functions.https.onRequest((req, res) => {
  const value = req.query.addValue;
  admin.database().ref('users/{userId}').push({value}).then(snapshot => {
  res.redirect(303, snapshot.ref); });
});

问题:如何使此功能将数据推送给所有用户?

Question: how to make this function push the data to all users?

推荐答案

在触发器中连接通配符的语法不适用于构建数据库引用的路径.在那里,您必须使用常规的字符串插值:

The syntax for wiring up a wild-card in a trigger, does not apply for building a path to a database reference. There you'll have to use regular string interpolation:

exports.addEntry = functions.https.onRequest((req, res) => {
  const value = req.query.addValue;
  admin.database().ref('users/'+userId).push({value}).then(snapshot => {
  res.redirect(303, snapshot.ref); });
});

或ES6模板文字:

exports.addEntry = functions.https.onRequest((req, res) => {
  const value = req.query.addValue;
  admin.database().ref(`users/${userId}`).push({value}).then(snapshot => {
  res.redirect(303, snapshot.ref); });
});

这篇关于Firebase Cloud功能:如何在http请求中使用通配符推送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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