Friday, 12 May 2017

Random Cannot read property of null

I am new to nodejs and I am trying to do a rss parser.

I use mongoose for store articles and rss-parser for convert xml to json.

I don't know why, but I have the error : "Random Cannot read property of null" randomly. Some times the code works, some times not.

/* lib require */
var parser = require('rss-parser');

var mongoose = require('mongoose');

/* Mongodb connection*/

mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/rss', function(err) {
    if (err) { throw err; }
});

/* Schema for mongoose */
var articleSchema = new mongoose.Schema({
    title : String,
    pubDate : String,
    category: String,
    date : { type : Date, default : Date.now }
});

var articleModel = mongoose.model('articles', articleSchema);

/* Array of rss to check */ 
var rssurl = [];

rssurl.push({url: "URL", 
webhook: "LINK";,
category : "CATEGORY"});

rssurl.push({url: "URL", 
webhook: "LINK", 
category: "CATEGORY"});



 /* For each feed, I check if the article exist */

rssurl.forEach(function(item) {
    parser.parseURL(item.url, function(err, parsed) {
        parsed.feed.entries.forEach(function(entry) {
            articleModel.find().count(function(err, count){
                if (count <= 0) {
                    console.log("check");
                }
            })
        })
    })
})



via irshlisht44