最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - firebase last record only - Stack Overflow

matteradmin2PV0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 13

It 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

Post a comment

comment list (0)

  1. No comments so far