2016-07-20 3 views
1

Meteor 비동기 메소드에서 "읽을 수있는"이벤트에서 콜백 함수를 호출하도록 설정했습니다. 그러나 콜백은 "읽기 가능"상태에서 시작될 때 호출되지 않습니다 (나는 콘솔에서 해고된다는 것을 알고 있습니다. 설정 한 로그).Meteor js 콜백이 작동하지 않습니다.

여기에 뭔가가 있습니까? 나는 2,3 시간 동안 지금 그것에 다른 약간의 물건을 시험해보고 있었다!

Meteor.startup(() => { 

    Meteor.call("getfeed", function(feedloader) { 
    //I get: TypeError: undefined is not a function] 
    console.log(feedloader); 
    }); 

}); 

Meteor.methods({ 

    getfeed: function(callb) { 

    var req = request('http://feeds.feedburner.com/Techcrunch'); 
    var feedparser = new FeedParser(); 
    testing = []; 

    //........a bunch of functions........ 

    feedparser.on('readable', function() { 

     var stream = this 
     , meta = this.meta 
     , item; 

     while (item = stream.read()) 
     { 
     //I'm pushing the results into testing var 
     testing.push(item); 
     } 

     //From the logs I can see that this is called 12 times 
     //but the callback's not firing!!! 

     console.log(testing.length); 
     callb(testing); 

    }); 
    } 
}); 

답변

1

유성 방법은 당신이 방법을 "호출"때 당신이 그것을 통과에도 불구하고 콜백 인수를하지 않는다는 의미에서 비동기 기능하지 않습니다. 대신 각 메서드는 Fiber 내에서 실행되며 이는 비동기 코드 처리의 또 다른 특징입니다.

다행히 Meteor에는 두 가지 스타일을 혼합 할 수있는 훌륭한 도우미가 있습니다. 당신이해야 할 일은 Meteor.wrapAsync으로 메소드 코드의 "순수한"비동기 부분을 랩핑하는 것입니다. 이 구조는 다음과 같이 보일 것입니다 :

Meteor.methods({ 

    getfeed: function() { 
    var wrapped = Meteor.wrapAsync(function (callb) { 

     var feedparser = new FeedParser(); 
     testing = []; 

     // ... 

     feedparser.on('readable', function() { 
     // probably the same code you have, but without "callb()" 
     }); 

     feedparser.on('end', function() { 
     // NOTE: No error here, so the first argument must be null. 
     callb(null, testing); 
     }) 
    }); 

    // NOTE: Finally, call the wrapped function 
    return wrapped(); 
    } 
}); 
+0

안녕하세요, 여기서 문제는 '끝'이벤트가 없습니다. 여기서 '읽을 수있는'이벤트는 반복적으로 멈출 때까지 호출되며, 멈출 때를 알지 못합니다! 변수와 동기화 된 'testing'변수를 유지할 수있는 방법이 있습니까? – jaisonDavis

+0

죄송합니다. 종료 이벤트가 있습니다. 그것을 놓쳤다! – jaisonDavis

+0

그리고 네가 돌아 오는 포장해야합니다(); 그냥 wrapped() 대신에; – jaisonDavis

관련 문제