【问题标题】:How to retrieve a value from Firebase DB with Admin- TS如何使用 Admin-TS 从 Firebase DB 检索值
【发布时间】:2019-01-22 00:16:39
【问题描述】:

我想将用户的分数保存在我的实时数据库中。我使用 Cloud Functions-打字稿。

//MY DB:    
"users": {
    "Michael": {
                 "scores": "1000"
               }
          }

我在管理端的代码(云功能)

exports.gettingValueFromDB = functions.https.onCall(async (data, context) => 
{

if (context.auth!==null)
{ 
const uid = context.auth.uid;
const ref =  admin.database().ref(`/users/${uid}/`);

//???
//const scores= ????   
}


});

【问题讨论】:

    标签: typescript firebase firebase-realtime-database google-cloud-functions


    【解决方案1】:

    初始化:

    var db = admin.database();
    var uid = 'Michael';    
    var usersRef = db.ref(`users/${uid}`);
    

    查询一次:

    usersRef.once("value", function(snapshot) {
      val = snapshot.val();
      console.log(val.scores);
    });
    

    异步监听器:

    usersRef.on("value", function(snapshot) {
      val = snapshot.val();
      console.log(val.scores);
    });
    

    参考:Retrieving data from Firebase

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-17
      • 1970-01-01
      • 2021-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多