GeoFire在Firebase实时数据库中的查询不起作用。

huangapple go评论78阅读模式
英文:

GeoFire query in Firebase Realtime Database is not working

问题

我正在使用GeoFire在我的React Native应用中向Firebase实时数据库添加和检索位置数据。使用addDataToGeofire()函数成功添加数据,但当我尝试使用getDataFromGeofire()函数检索数据并使用geoFire.query进行查询时,我收到以下错误消息:

"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]"

geoFire.get()函数正常工作,可以返回特定键的位置数据。然而,geoFire.query()函数根本不起作用。我已经检查了Firebase实时数据库中的数据结构,添加数据后看起来像这样:

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

我不确定是什么导致了geoFire.query()函数的错误。有人可以帮助我解决这个问题吗?以下是我的代码:

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

完整的输出:

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.

提前感谢您的帮助。

英文:

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

答案1

得分: 1

根据错误提示,你正在查询数据的路径,但是你没有在数据库规则中定义索引

如果你确实将数据直接存储在根目录下(这很不常见,但不是不可能的),索引应该如下所示:

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

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"
  }
}

huangapple
  • 本文由 发表于 2023年2月27日 03:57:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574656.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定