0

I have to perform a Query using Cloud Function in my Firebase Database and get me back a list of Objects in Json format.

But at the moment I can't even Query properly...

I have a List of UserIds under a Node, and under each UserId Node is a List of Objects where I need to grab the one that have a certain value.
It goes like this:

"rewardTransactions" : {
"Lu9XzBzGK2Q42ml8TstACwzusq1" : {
  "-LDeJgAYjDBDRqa4E7Hn" : {
    "transaction_date" : "9/3/18",
    "transaction_points" : "400",
    "transaction_recipient_index" : "2",
    "transaction_status" : "PROCESSED",
    "transaction_value" : "$40.00"
  },
  "-LDeJhvkRp25k_0xpP77" : {
    "transaction_date" : "20/4/18",
    "transaction_points" : "100",
    "transaction_recipient_index" : "2",
    "transaction_status" : "PENDING",
    "transaction_value" : "$10.00"
  }
},
"W6RTEnn7haN937HaPmwKXpZPN7f1" : {
  "-LDeJgg1pi00IvceTFIi" : {
    "transaction_date" : "16/3/18",
    "transaction_points" : "300",
    "transaction_recipient_index" : "1",
    "transaction_status" : "PROCESSED",
    "transaction_value" : "$30.00"
  },

I need to grab all the Transactions that have a "transaction_status" value of "PENDING". But so far, no success.

Here is my Cloud Function:

export const getPendingTransactions = functions.https.onRequest((request,response) =>{

const ref = admin.database().ref('/rewardTransactions/');
const pendingTransactionsQuery = ref.orderByChild('transaction_status').equalTo("PENDING");

return pendingTransactionsQuery.once('value').then(result =>{
    console.log(result.val());

 });

});
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Andy Strife
  • 729
  • 1
  • 8
  • 27
  • What does "no success" mean? When you execute this code, what happens? What happens if you `catch()` and log errors, in addition to your existing `then()`? Did you try running the equivalent query in a local Node.js script, to see if it works there? – Frank van Puffelen Jun 06 '18 at 02:22
  • Hi Puff! Thanks for jumping in! The output in the Firebase Functions Logs Console is "null". No catch errors (I added it afterwards to get more feedbacks).Is this the correct way to perform a Query in Cloud Functions? – Andy Strife Jun 06 '18 at 02:52
  • Ah, I see now that you're double nesting the data. The Firebase Database can only order/filter on values that are at a known path under each direct child. See https://stackoverflow.com/questions/27207059/firebase-query-double-nested – Frank van Puffelen Jun 06 '18 at 03:39
  • Oh, I see now. I will then grab the firebaseUserIds into an array and then query based on those IDs. Thanks a lot Puff and keep up with the great work!! – Andy Strife Jun 06 '18 at 04:00

0 Answers0