2017-05-12 6 views
0

저는 nodejs를 처음 사용하며 rss 파서를 사용하려고합니다.무작위로 null 속성을 읽을 수 없습니다.

나는 상점 기사에 mongoose를 사용하고 xml을 json으로 변환하기 위해 rss-parser를 사용한다.

왜 그런지 모르겠지만 "임의의 속성을 임의로 읽을 수 없습니다"라는 오류가 있습니다. 코드가 작동하는 경우도 있고, 그렇지 않은 경우도 있습니다.

/* 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"); 
       } 
      }) 
     }) 
    }) 
}) 

편집 :

TypeError: Cannot read property 'feed' of null

at ~/node_modules/rss-parser/index.js:192:15

답변

0

몽구스가 연결이 성공하기 전에 모델링을 시작할 수 :이 오류입니다. 실제로 작업을 수행하지 않도록 이 어느 연결이 나중에 또는 연결 속도가 느린을 실패 : 오류가 단지 내 의혹은 두 가지 중 하나입니다, 때로는 일이 말 때문에

당신은 당신이 데이터를 질의 할 때가되기를 원한다. 어느 경우

, 당신은 연결이 성공 때까지 작업을 지연 시도해 볼 수도 있습니다 : 코드는 항상 성공

mongoose.Promise = global.Promise; 
mongoose.connect('mongodb://localhost/rss').then(() => { 
    var articleModel = mongoose.model('articles', articleSchema); 
    ... 
}, (err) => { 
    console.log("failure connecting"); 
    throw err 
}); 

경우, 나는 그것을에서 결과를 데이터베이스에 연결하고 쿼리 사이의 경쟁 조건 용의자 그것. 오류 상태가 발생하면 처음부터 연결 문제가 있음을 알 수 있습니다.

+0

감사합니다. 죄송합니다. 오류 메시지를 잊어 버렸습니다. "TypeError : null의 'feed'속성을 읽을 수 없습니다. 귀하의 대답은 이것에 대해 작동합니까? – irshlisht44

+0

그럴 경우,'parseURL' 가능성이 높습니다. parser.parseURL (item.url, function (err, parsed))에서'err'의 값을 출력 할 수 있습니까? – AnilRedshift

+0

아무 것도 출력하지 않습니다. 직접 오류가 있습니다. – irshlisht44

관련 문제