简体   繁体   English

如何使用Google云功能从Firebase实时数据库中读取值?

[英]How to read a value from Firebase realtime database with a Google cloud functions?

I'm starting to use firebase cloud functions, and I have trouble reading an entry "Hello" from my database tree : 我开始使用Firebase云功能,并且无法从数据库树中读取条目“ Hello”

在此处输入图片说明

I'm trying to read the "Hello" value inside HANDLE/suj1/part1 from my tree. 我正在尝试从我的树中读取HANDLE / suj1 / part1中“ Hello”值。 I am using a firebase cloud function that is triggered when I create an other entry with an IOS Application in the database inside "INTENT" . 我正在使用Firebase云功能,该功能在“ INTENT”内部的数据库中使用IOS应用程序创建另一个条目时触发。 The functions gets called well but every time I try to read the "Hello" value , it returns a null value in my firebase console where I expect it to return "Hello". 该函数调用得很好,但是每次我尝试读取“ Hello”值时,它都会在我的Firebase控制台中返回一个空值,我希望它在其中返回“ Hello”。

Here is the code I use : 这是我使用的代码:

const functions = require('firebase-functions')
const admin = require('firebase-admin')
admin.initializeApp()

exports.match = functions.database.ref('INTENT/{userid}').onCreate(snapshot => {

const user = snapshot.key


return admin.database().ref('HANDLE/suj1/part1').once('value', (snap) => {
    const hello = snap.val()
    console.log(hello) // Null
 });

Can someone tell what am I doing wrong ? 有人可以告诉我我在做什么错吗?

I found out with this line and Frank's help: 我发现这一行和弗兰克的帮助:

admin.database().ref().once('value', (snap) => { console.log(JSON.stringify(snap.val())); });

That I had added a whitespace at the end of "HANDLE " in my path, which does not appear in the Firebase console. 我在路径中“ HANDLE”的末尾添加了一个空格,该空格未出现在Firebase控制台中。 I had to delete the branch and create an other one. 我必须删除分支并创建另一个分支。

Try this: 尝试这个:

const functions = require('firebase-functions')
const admin = require('firebase-admin')
admin.initializeApp()

exports.match = 
functions.database.ref('OTHERLOCATION/{userid}').onCreate((snapshot) => {
const user = snapshot.key

return admin.database().ref().child('HANDLE/suj1').once('value', function(snap) => {
const hello = snap.val().part1
console.log(hello) // "Hello"
});

please try this 请尝试这个

const userName = admin.database().ref('/HANDLE/suj1/part1').once('value',function(snapshot) {

const hello = snapshot.val();
console.log(hello);

});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase云功能只是拒绝从实时数据库读取 - Firebase Cloud Functions Simply Refuse to Read From Realtime Database 如何触发Google Cloud服务器中的实时数据库字段(不在Firebase的功能中) - How to trigger realtime database field in Google Cloud server (not in functions of Firebase) Firebase的Cloud Functions:如何从数据库同步读取数据? - Cloud Functions for Firebase: how to read data from Database synchronously? 如何将 Firebase 云函数设置为路由到 Firestore 而不是实时数据库 - How to set Firebase cloud functions to route to Firestore instead of Realtime Database Firebase 实时数据库事务和云功能 - Firebase realtime database transaction and cloud functions 如何使用 Firebase 函数中的 firebase 实时数据库? (电报机器人) - How to work with firebase realtime database from Firebase Functions? (Telegram Bot) Google Cloud Functions +实时数据库非常慢 - Google Cloud Functions + Realtime Database extremely slow 如何从云功能中止到Firebase实时数据库的创建? - How to abort creation into firebase realtime database from a cloud function? 如何从Firebase数据库获取值并将其分配给Firebase Cloud Functions中的值? - How to get a value from Firebase Database and assign it to a value in Firebase Cloud Functions? 如何在使用 Cloud Functions 时使用 Firebase Admin SDK 更改 Firebase 实时数据库中的数据? - How do I use Firebase Admin SDK to change data in Firebase Realtime Database while using Cloud Functions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM