【问题标题】:Skip Duplicates in Firebase Database跳过 Firebase 数据库中的重复项
【发布时间】:2016-06-24 03:37:31
【问题描述】:

我正在从 RSS 提要中检索数据,并将其作为结构化日期添加到 Firebase 的数据库中。每次刷新 RSS 提要时,都会添加一个新的 UID,但问题是它添加了相同的值。换句话说:

有没有办法检查一个值,例如更新:“Fish shows on all..”已经存在并跳过添加一个全新的 UID?

我使用feedparser (node.js) 进行 rss 解析,这是导致每次刷新时重复的代码:

feedparser.on('readable', function() {
  // This is where the action is!

  var stream = this
    , meta = this.meta // **NOTE** the "meta" is always available in the context of the feedparser instance
    , item;

  while (item = stream.read()) {

      console.log(item);


var pubDate = item.pubDate;
    var date = new Date(pubDate);
    var description = item.description;

var parts = description.split(' - ', 2);

var time = parts[0];
var update  = parts[1];


  //  date.setTimezone  
  //  var time = date.getHours()+":"+date.getMinutes();


      var monthNames = ["January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"
                       ];

      var month = monthNames[date.getMonth()]+" "+date.getDate();

        var postData = {
        time: time,
        date: month,
        update: update,
        description: description,
        day: pubDate

  };


var newPostKey = firebase.database().ref().child('feed').push().key;
  var updates = {};
  updates['/feed/' + month + '/' + newPostKey] = postData;
  return firebase.database().ref().update(updates);

【问题讨论】:

标签: node.js firebase firebase-realtime-database


【解决方案1】:

我认为,这里的解决方案是使用解析的提要数据来获取某种唯一标识符,并将其用作您的 Firebase 数据库密钥,而不是使用推送。例如,许多 RSS 提要都有一个 guid 值来唯一标识一个帖子,因此您可以执行以下操作:

firebase.database().ref('feed')
  .child(month).child(postData.guid)
  .update(updates);

这样,如果您多次获取同一个项目,只要 guid 相同,它将始终就地更新。

【讨论】:

  • 我不敢相信我没有想到这个!谢谢你,先生!
猜你喜欢
  • 2020-06-19
  • 2013-01-13
  • 1970-01-01
  • 2012-10-06
  • 2020-09-08
  • 1970-01-01
  • 1970-01-01
  • 2016-08-24
  • 1970-01-01
相关资源
最近更新 更多