最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - firebase last record only - Stack Overflow

programmeradmin4浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论