简体   繁体   中英

how to read data from firebase with admin sdk in cloud functions with typescript

I am trying to write a cloud function which will be triggered by http and I will send a date as input and based on that date I want to fetch the list of games in my firebase db.

I am not sure how to fetch the data from the snapshot i am receiving from promise.

Code:

enter code hereexport const checkGameResult = 
functions.https.onRequest((request, response) => {
  const date:string = request.query.gameDate;
  console.log(date);
  admin.database().ref('activegames/'+date).once('value').then(result=>{
    console.log('result: ');
    console.log(result);
    console.log('result[0]: '+result[0]);
    response.send(date);
  }).catch(error=>{
    response.status(500).status(error);
  });
});

DB stucture: 数据库结构:

Console output: 在此处输入图片说明

I am not sure how to fetch the data from the snapshot i am receiving from promise. Please advise.

Call val() on the snapshot to get a raw JavaScript object with the contents of the location of the reference. Use that to send back the data you want.

admin.database().ref('activegames/'+date).once('value').then(result=>{
  console.log('result: ');
  console.log(result.val());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM