I want to append only the last record added to firebase to a table. I tried
firebase().database().ref()
.orderByChild("dateAdded")
.limitToLast(1)
.on("child_added", function (snapshot) {
$('.table').append(
"<tr><td>" + snapshot.val().train +
"</td><td>" + snapshot.val().destination +
"</td><td>" + snapshot.val().frequency +
"</td></tr>"
);
});
The first time I add a new entry it works but the second new entry is added twice, third - three times, etc.
Is there a way that will only add each new entry only once?
I want to append only the last record added to firebase to a table. I tried
firebase().database().ref()
.orderByChild("dateAdded")
.limitToLast(1)
.on("child_added", function (snapshot) {
$('.table').append(
"<tr><td>" + snapshot.val().train +
"</td><td>" + snapshot.val().destination +
"</td><td>" + snapshot.val().frequency +
"</td></tr>"
);
});
The first time I add a new entry it works but the second new entry is added twice, third - three times, etc.
Is there a way that will only add each new entry only once?
Share Improve this question edited Mar 14, 2023 at 19:50 Renaud Tarnec 83.2k10 gold badges98 silver badges129 bronze badges Recognized by Google Cloud Collective asked Mar 20, 2018 at 18:57 PaulaMacTPaulaMacT 771 silver badge8 bronze badges1 Answer
Reset to default 13It seems you need to read your data only once and not every time data is changed at the specified database reference
So you should do:
firebase().database().ref()
.orderByChild("dateAdded")
.limitToLast(1)
.once("child_added....
Have a look at the documentation: https://firebase.google./docs/database/web/read-and-write#read_data_once
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743624555a4480325.html
评论列表(0条)