Firebase数据库字段上的多个查询

时间:2019-12-10 17:19:21

标签: react-native firebase-realtime-database

我目前正在开发react-native待办事项应用程序,您可以在其中添加任务,我只想显示尚未完成或归档的任务,同时在查询两个字段时遇到一些困难,这是我查询字段的代码。

const { currentUser } = firebase.auth();
    // fetch all tasks
    firebase
    .database()
    .ref(`/users/${currentUser.uid}/tasks`) 
    .ref.orderByChild("completed").equalTo(false)
    //.ref.orderByChild("archived").equalTo(false) - This query is not working
    .on("value", snapshot =>
      this.setState({ tasks: snapshot.val(), loading: false })
    );
  }

我可以在如何检查两个字段方面获得一些帮助吗?我想检查这些字段,因为如上所述,我只想在尚未完成或未归档的默认视图上显示任务。我已经看过以前的相关线程,并且我绝对不想因为这个原因而更改数据库结构,而是将任务呈现为平面列表,可以在下面找到:

 _FlatListData = data =>
    // data recieved from firebase is (collection) an object with individual tasks
    // as separate objects with firebase-defined-id as the key and actual task object as value
    // Object.entries breaks down the collection into an array of arrays with 2 elements each
    // [0] as the key and [1] as value which is then mapped to get an array of objects combining
    // the data and the key value into a single object
    Object.entries(data)
      .map(d => {
        return { ...d[1], key: d[0] };
      })
      .reverse();

{/* render FlatList only if state.tasks is !empty*/}

        {!!this.state.tasks && (
          <FlatList
            /* Since the data fetched from firebase is not an array but an 
            object it needs to be converted into a workable array first */
            data={this._FlatListData(this.state.tasks)}
            renderItem={({ item }) => (
              <TaskCard
                data={item}
                DeleteTask={this.DeleteTask}
                UpdateTask={this.UpdateTask}
              />
            )}
            keyExtractor={item => item.key.toString()}
          />
        )}

编辑: 这是示例json,其结构如下:

{
  "users" : {
    "E9JRDTPLZrSAfc0ERvO0yHJw3am1" : {
      "tasks" : {
        "-LvkpiUO_uxXcf-BYZqk" : {
          "archived" : false,
          "body" : "Test",
          "completed" : true,
          "name" : "Test",
          "timestamp" : 1575998646319
        },
        "-Lvl3qzXylp5H2oOLiaE" : {
          "archived" : true,
          "body" : "Gh",
          "completed" : false,
          "name" : "Gg",
          "timestamp" : 1576002613305
        },
        "-Lvl4YE2E_omKrsOjkZs" : {
          "archived" : false,
          "body" : "Hu",
          "completed" : true,
          "name" : "Bh",
          "timestamp" : 1576002794529
        },
        "-Lvl4woHR5rhtl_JgkBP" : {
          "archived" : true,
          "body" : "Vc",
          "completed" : false,
          "name" : "Bbj",
          "timestamp" : 1576002899308
        }
      }
    },
    "ZqABgFit8YglimXPPZJNoSeea803" : {
      "tasks" : {
        "-Lvg7rT7PfFLx1nzEzgg" : {
          "archived" : false,
          "body" : "Test\nS\nS\nS\nS\nDkdndodn\n\n",
          "completed" : true,
          "name" : "Twdt",
          "timestamp" : 1575919777699
        }
      }
    }
  }
}

0 个答案:

没有答案