代码之家  ›  专栏  ›  技术社区  ›  Sandra Willford

AngularFireBase2查询,其中键等于值

  •  1
  • Sandra Willford  · 技术社区  · 6 年前

    我在FireBase上开枪,将现有数据库转换为FireBase。如何选择键值等于特定值的列表?

    getAllUsers() {
        return this.fireBase.list(
            '/users',
            ref => ref
                .orderByChild('last_name')
                // active = true
        ).valueChanges();
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Renaud Tarnec    6 年前

    使用FireBase实时数据库时,不能使用 orderBy() 在一个字段上,然后使用 equalTo() 在另一个领域。你不能用两种不同的 命令字节() .

    这在文件中有详细说明。 here (角火焰2) ) here (“标准”JavaScript SDK)。

    但是,可以使用组合值,例如 active_lastname 使用如下查询:

      var database = firebase.database();
      database
        .ref('users')
        .orderByChild('composedValue')
        .startAt('true_')
        .endAt('true_\uf8ff')
        .once('value', function(snapshot) {
          snapshot.forEach(function(childSnapshot) {
            var childKey = childSnapshot.key;
            var childData = childSnapshot.val();
            console.log(childKey);
            console.log(childData);
          });
        });
    

    您编写的值如下:

    true_Obama
    true_Bush
    false_Carter
    ....
    

    因此,对于angularfire2,根据您的代码,它将是:

    getAllUsers() {
        return this.fireBase.list(
            '/users',
            ref => ref
                .orderByChild('composedValue')
                .startAt('true')
                .endAt('true\uf8ff')
        ).valueChanges();
    }