I have a diagram in Firebase that looks like this:
messages/
$groupId/
$messageId/
message: 'Sample Message'
createdBy: 'userID'
createdAt: 1513337977055
Then, in my code, the following queries are executed sequentially:
ref.child('messages/$groupId/$messageId')
.once('value')
.then(snap => console.log('value', snap.val()))
ref.child('messages/$groupId')
.orderByKey()
.limitToLast(1)
.on('child_added', snap => console.log('child_added', snap.val()))
I am wondering why it child_added
is called twice here, the first of which is similar to the value returned by the request once('value')
.
The console is displayed here:
child_added { message: 'Hello', createdAt: 1513337977055, createdBy: 'userId' }
value { message: 'Hello', createdAt: 1513337977055, createdBy: 'userId' }
child_added { message: 'Another message', createdAt: 1513337977066, createdBy: 'userId2' }
Please note that I am not adding new entries to Firebase here. Just a request.
EDIT: here is the link to the fiddle demonstrating the problem: https://jsfiddle.net/dspLwvc3/2/
source
share