Query a FirebaseListObservable and then push a new element
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.
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#
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 .
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 } }); }