如何使用Admin

时间:2019-01-12 01:59:51

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

我想将用户的分数保存在我的实时数据库中。我使用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= ????   
}


});

1 个答案:

答案 0 :(得分:0)

初始化:

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