flipcode
flipcode

Reputation: 671

AngularFire2 orderByKey and startAt

I have the following Database structure:

{
 "users" : {
    "ap1-AXLKSD9ASLKDJJ0991" : {
      "enabled" : true,
      "color" : "red"
    },
    "ap1-A778ASDUASDJ8AS8DA" : {
      "enabled" : true,
      "color" : "blue"
    },
    "ap2-ASD88ASD8ASD8HAS8D" : {
      "enabled" : true,
      "color" : "red"
    }
  }
}

I'm trying to get all users who have a key starting with "ap1-" as follows:

import { Injectable } from '@angular/core';

import { AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database';

import * as firebase from 'firebase/app';

import { Observable } from 'rxjs/Observable';

@Injectable()
export class FirebaseService {
    //
    // Database
    userList: FirebaseListObservable<any>;

    public constructor(
        public afDb: AngularFireDatabase) {
    }

    getUsers() {
        return this.userList = this.afDb.list('/users', {
            preserveSnapshot: true,
            query: {
                orderByKey: true,
                startAt: 'ap1-'
            }
         });
    }
}

The above code however, returns ap1- and ap2- keys. I'm searched online and cannot find any way to achieve this.

I tried with equalTo.. when I put in the exact correct key, it finds only this. When I put in a nonsense key it finds nothing.

It seems like there's a bug in startAt?

Upvotes: 1

Views: 1143

Answers (1)

avilao
avilao

Reputation: 193

You cannot do it with firebase like that. What 'startsWith' does is to retrieve all the keys from 'ap1-' and above, not the strings that have 'ap1-' as first characters.

If you had an the following keys

{"1", "2", "3", "4"}

and queried it like

startsWith: "3"

then you would get

{ "3", "4" }

Upvotes: 2

Related Questions