跳过Firebase数据库中的重复项 [英] Skip Duplicates in Firebase Database

查看:39
本文介绍了跳过Firebase数据库中的重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm retrieving data from an RSS feed and adding in as structured date in Firebase's database. Every time the RSS feed is refreshed, a new UID is added but the issue is it adds the same values. In other words:

有没有一种方法可以检查一个值,例如更新:鱼在所有上显示."已经存在,请跳过而改为添加一个新的UID?

Is there a way to check if a value, e.g. update: "Fish showing on all.." already exists and skip instead adding a whole new UID?

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

I'm using feedparser (node.js) for the rss parsing and this is the code causing the duplicates on every refresh:

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);

推荐答案

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

The solution here, I think, is to use the parsed feed data to get some kind of unique identifier and use that as your Firebase Database key instead of using push. For example, many RSS feeds have a guid value that uniquely identifies a post, so you might be able to do something like:

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

这样,如果您多次提取同一项目,只要 guid 是相同的,它将始终就地更新.

That way, if you fetch the same item multiple times so long as the guid is the same it will always update in-place.

这篇关于跳过Firebase数据库中的重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆