0

Needed Details

I am using Dialogflow fulfillment for my project along with Firebase realtime database. My database has a structure: enter image description here

Let me first tell you about the structure of the database. It has got a list of ATMs(automated teller machine) with its full address and other needful information. I need to fetch all the ATMs with a given value of Pin number, and the given value of Pin number will come from an API response. This Pin number is nothing but the Zip Code for that ATM. So obviously, with a Pin number, there can be many ATMs.

My problem

Below is the code I am using to make an API call and Firebase database querying with received Pin value as the response from API.

var ref = db.ref("atms/");

var response = await axios.get('some URL');   //API call 
var zipcodes = response.data.search_results;   // Array of Pin values
console.log(zipcodes[2].postal_code);  //This prints a Pin value 721304, which confirms successful response from API

ref.orderByChild("Pin").equalTo(zipcodes[2].postal_code).on("child_added", function(snapshot) {   //this line is the problem
   console.log(snapshot.val());
});

But the above code with equalTo(zipcodes[2].postal_code) does not give any response. But when I replace it with equalTo(721304), it does give an expected response. To emphasize, both equalTo(zipcodes[2].postal_code) and equalTo(721304) are necessarily same.

I need to use this equalTo(zipcodes[2].postal_code) only because I will be running a loop over this query, which is because of the need to query many, many Pin at once.

Could you please help me understand what is wrong here and how do I implement what I am trying to do here? Please let me know in case of any follow-up questions.

Shivam
  • 192
  • 1
  • 12

1 Answers1

1

As shown by console.log(typeof zipcodes[2].postal_code); (see comments above), the value of zipcodes[2].postal_code is not of type number but of type string. You need to convert it to number, as follows:

ref.orderByChild("Pin").equalTo(Number(zipcodes[2].postal_code)).on("child_added", function(snapshot) {   //this line is the problem
   console.log(snapshot.val());
});
Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121