Search code examples
javascripttypescriptfirebasefirebase-realtime-databasegeofire

GeoFire query in Firebase Realtime Database is not working


I am using GeoFire to add and retrieve location data in Firebase Realtime Database in my React Native app. The data is added successfully using the addDataToGeofire() function, but when I try to retrieve the data using the getDataFromGeofire() function and query it using geoFire.query, I get the following error message:

"ERROR [Error: Firebase Database (${JSCORE_VERSION}) INTERNAL ASSERT FAILED: Unknown node type] WARN [@firebase/database: FIREBASE WARNING: Using an unspecified index. Your data will be downloaded and filtered on the client. Consider adding ".indexOn": "g" at / to your security rules for better performance. ERROR [TypeError: t.split is not a function. (In 't.split("/")', 't.split' is undefined)] ERROR [Error: Firebase Database (${JSCORE_VERSION}) INTERNAL ASSERT FAILED: Unknown node type] WARN [@firebase/database: FIREBASE WARNING: Using an unspecified index. Your data will be downloaded and filtered on the client. Consider adding ".indexOn": "g" at / to your security rules for better performance]"

The geoFire.get() function is working fine and returns the location data for a specific key. However, the geoFire.query() function is not working at all. I have checked the data structure in the Firebase Realtime Database and it looks like this after adding data with geoFire.set():

{ "-NP4heZ1yLXk96HiScqH": { ".priority": "u2mw1ze54y", "g": "u2mw1ze54y", "l": [ 40.502686, 19.0655181 ] } }

I am not sure what is causing the error in the geoFire.query() function. Can anyone help me with this issue? Here is my code:

firebasehelper.tsx:

public async addDataToGeofire(){
  const geoFire = await new GeoFire(this.firebaseRef);

  const requestId = "-NP4heZ1yLXk96HiScqH";
  const latitude = 40.502686;
  const longitude = 19.0655181;

  await geoFire.set(requestId, [latitude, longitude])
    .then(() => {
      console.log(`Coordinates successfully added to the key: ${requestId}`);
    })
    .catch((error) => {
      console.error(`An error occurred while adding the coordinate" ${error}`);
    });
}

public async getDataFromGeofire(){
  const geoFire = await new GeoFire(this.firebaseRef);

  await geoFire.get("-NP4heZ1yLXk96HiScqH").then(function(location) {
    if (location === null) {
      console.log("Provided key is not in GeoFire");
    }
    else {
      console.log("Provided key has a location of " + location);
    }
  }, function(error) {
    console.log("Error: " + error);
  });

  const geoQuery = geoFire.query({
    center: [40.502686, 19.0655181],
    radius: 100
  });

  const onReadyRegistration = geoQuery.on("ready", () => {
    console.log("GeoQuery has loaded and fired all other events for initial data");
  });
}

app.tsx:

    constructor(){
  this.someMethod();    
}

async someMethod(){
  await databaseHelper.addDataToGeofire().then(async () => {await databaseHelper.getDataFromGeofire()});
}

the whole output:

LOG Coordinates successfully added to the key: -NP4heZ1yLXk96HiScqH LOG Provided key has a location of 40.502686,19.0655181 ERROR [Error: Firebase Database (${JSCORE_VERSION}) INTERNAL ASSERT FAILED: Unknown node type] WARN [2023-02-26T19:54:41.545Z] @firebase/database: FIREBASE WARNING: Using an unspecified index. Your data will be downloaded and filtered on the client. Consider adding ".indexOn": "g" at / to your security rules for better performance. ERROR [TypeError: t.split is not a function. (In 't.split("/")', 't.split' is undefined)] ERROR [Error: Firebase Database (${JSCORE_VERSION}) INTERNAL ASSERT FAILED: Unknown node type] WARN [2023-02-26T19:54:41.698Z] @firebase/database: FIREBASE WARNING: Using an unspecified index. Your data will be downloaded and filtered on the client. Consider adding ".indexOn": "g" at / to your security rules for better performance.

Thank you in advance


Solution

  • As the error says, you're querying data at a path for which you didn't define an index your in your database's rules.

    If you really stored the data directly under the root (that is uncommon, but not impossible) the index would be:

    {
      "rules": {
        ...,
        ".indexOn": "g"
      }
    }