简体   繁体   English

使用带有Javascript的infiniteScroll过滤Firebase数据集

[英]Filtering Firebase datasets with infiniteScroll with Javascript

I'm using Javascript (Ionic Framework) to load groups of Firebase sets (like Instagram), but I need to also filter the Firebase sets based on the value of the "sold" key in each data set. 我正在使用Javascript(Ionic Framework)加载Firebase集组(如Instagram),但我还需要根据每个数据集中“ sold”键的值过滤Firebase集。 Here is one data set (there are many such sets): 这是一个数据集(有很多这样的数据集):

在此处输入图片说明

The idea is if "sold" is false, then we load a given data set, if it's true, then we should not load it and skip it. 这个想法是,如果“ sold”为假,则我们加载给定的数据集,如果为真,则不应加载并跳过它。

But there is a catch which makes this problem difficult. 但是有一个问题使这个问题变得困难。 Please see the code next: 请参见下面的代码:

  doInfinite(infiniteScroll) {                                   
    if (this.end === false && (this.counter === 3 || this.counter === 8) && this.startListingID != undefined) {
      setTimeout(() => {                                                                              
        firebase.database().ref('/explore/').orderByKey().startAt((this.startListingID).toString()).limitToFirst(6).once('value', (snapshot) => {
          snapshot.forEach(snap => {         
            if((this.counter <= (this.counter2 + 4)) && (this.intermediateListingID !== snap.val().listingID) && this.end === false) {
              let temp = {
                Item: snap.val().Item,
                Price: snap.val().price,
                displayName: snap.val().displayName,
                sold: snap.val().sold,
                listingID: snap.val().listingID }
              this.object.push(temp);
              this.counter++;
              this.intermediateListingID = snap.val().listingID;
              return false;
            } else {
              if(this.startListingID !== snap.val().listingID) {
                this.counter = this.counter2;
                this.startListingID = snap.val().listingID;
              }
              else { this.end = true; }
            }
            setTimeout(() => {
              infiniteScroll.complete();
            }, 1000);
          });
          setTimeout(() => {
            infiniteScroll.complete();
          }, 1000);
        }).catch((error) => { console.log("infiniteScroll Error: " + JSON.stringify(error)); infiniteScroll.complete(); });
      }, 1000);
    } else {
      setTimeout(() => {
        infiniteScroll.complete();
      }, 1000);
    }
  }

I am just starting with coding, so I do realize the code above is rather bad, but it works. 我只是从编码开始,所以我确实意识到上面的代码是很糟糕的,但是它可以工作。

The problem arises in cases where I would get "sold" data sets, and then the infinite scroll which is limitToFirst(6) turns to be less and the whole numbering logic screws up, not to mention if I get 6 sold data sets in a row, then nothing is shown and the scroll just breaks down. 问题是在以下情况下出现的:我将获得“出售”的数据集,然后limitToFirst(6)的无限滚动变小并且整个编号逻辑limitToFirst(6)了,更不用说我是否在一个销售单中得到了6个出售的数据集。行,则什么也没有显示,并且滚动条只是分解了。

If there a way (ideally on the level of Firebase) to FILTER all data sets with "sold = true" before they are fed into my infinite scroll logic? 是否有办法(最好是在Firebase上)过滤所有具有“ sold = true”的数据集,然后再将它们馈入我的无限滚动逻辑中?

Many thanks! 非常感谢!

You could replace orderByKey with orderByChild('sold').equalTo(true) 您可以将orderByKey替换为orderByChild('sold')。equalTo(true)

firebase.database().ref('/explore/').orderByChild('sold').equalTo(true)
.limitToFirst(6).once('value', (snapshot) => {...

This should give you 6 items where sold = true . 这应该给您6件,其中sold = true Unfortunately, you can't combine two filtering queries. 不幸的是,您不能合并两个过滤查询。 So you'll either have to pull all the items and then just sort out which ones are sold after, or grab all the sold ones in random order. 因此,您要么必须拉出所有物品,然后才确定要出售的物品,要么以随机顺序抓住所有出售的物品。 Though, if you're sorting by key this leads me to believe you don't need them sorted in any particular order, so you could go with this. 但是,如果按键排序,则使我相信您不需要按任何特定顺序对它们进行排序,因此您可以这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM