[英]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.