Debanjan Banerjee
Debanjan Banerjee

Reputation: 73

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: DB stucture:

Console output: enter image description here

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

Upvotes: 2

Views: 2751

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317487

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());

Upvotes: 3

Related Questions