angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

Query a FirebaseListObservable and then push a new element

Open mquentin opened this issue 10 years ago • 3 comments

Hello,

I am trying to querry a FirebaseListObservable :

let objectList= this.af.database.list('/myObjects', { query: { orderByChild: 'title', equalTo: 'TEST' } });

And then, if this objectList is empty, I add a new object with a title='TEST' :

objectList.subscribe( snapshot => { if(null != snapshot && snapshot.length === 0){ this.af.database.list('/myObjects') .push({title:'TEST', content:'my content...'}); } });

When my list is empty, it does work but I already an Object with the title === 'TEST', I receive a first snapshot empty (so a new object with a title='TEST' is created) and then I receive the snapshot which is not empty...

I guess the problem comes from the Observable that I am updating within its own .subscibe... However, I do not find any other solution.

mquentin avatar Nov 14 '16 23:11 mquentin

Hey, I found a work around by working with FirebaseObjectObservable instead of FirebaseListObservable :
Instead of a query on the title, I use my title as a unique id :

let myObject= this.af.database.object("myObjects/"+ 'TEST'); myObject.subscribe(snapshot => { if(snapshot.$value === null){ myObject.set({title:'TEST', content:'my content...'}); } )};

cf : http://stackoverflow.com/questions/38422581/firebase-angularfire2-checking-if-user-exists-in-the-database#

mquentin avatar Nov 15 '16 00:11 mquentin

i have a problem

this.af.database.object('/Clientes/'+this.globalClienteId,{ preserveSnapshot: true}) .subscribe(snapshot=>{ console.log("key ="+snapshot.key); console.log("Value ="+snapshot.val());
});

snapshot.key return ok but snapshot.val() return null .

yuber1792 avatar Feb 17 '17 20:02 yuber1792

I also have problem, will anybody help to give alternate way to write the query for angularfire2 version 5, below query is working for version 4 but not in version 5. Please help.

getMessages(): FirebaseListObservable<ChatMessage[]> { query to create our message feed binding return this.db.list('messages', { query: { limitToLast: 25, orderByKey: true } }); }

shadabs22 avatar Feb 01 '18 18:02 shadabs22