0
을 Socket.IO에는 몽구스 ODM와 socket.io를 사용하여 시작하기 은 가정하자 내가 데이터베이스에서 데이터 (일부 제품)를 가져올 필요가 ... 문제에 온처리 몽구스와 응용 프로그램에서 콜백과
클라이언트 코드 :
socket.on('connect', function (data) {
socket.emit('fetch_articles',function(data){
data.forEach(function(val,index,arr){
$('#articlesList').append("<li>"+val.subject+"</li>")
});
});
});
및 서버 코드 :
var article_model = require('./models');
io.sockets.on('connection', function (socket) {
var articles = {};
// Here i fetch the data from db
article_model.fetchArticles().sort('-_id').limit(5).exec(function(err,data){
articles= data; // callback function
});
// and then sending them to the client
socket.on('fetch_articles', function(fn){
// Have to set Timeout to wait for the data in articles
setTimeout(function(){fn(articles)},1000);
});
});
그래서 내가 콜백 콜백 바로 실행 socket.on 같은 시간에 와야 데이터를 기다릴 필요가있다.
이 문제에 대한 간단하고 정확한 해결책이 있습니까? 당신이 원하는 것처럼
을 ... 다음 기사 VAR에 필요 ... 감사 – Roman
그것은 당신이 일을 끝낼 articles''에서 쿼리의 결과를 저장하지 않습니다, 후속 요청을 위해 재사용합니다. – robertklep