2012-06-11 26 views
0

檢索使用數據我有這樣的代碼從數據庫中使用JavaScript

App.Model('users').find(function (err, users) { 
     users.forEach(function(user) { 
     console.log(user.username); 
    }); 
}); 

//make usernames availible here. 

這個控制檯登錄的用戶名。 而不是僅僅記錄到控制檯我想使用這些數據。

但是怎麼辦呢?

感謝

+0

你想做什麼? – blockhead

+0

你想使用這些數據?那是什麼意思?你想建立一個用戶名列表嗎?或者可能是一個用戶對象列表? – lbstr

+0

分配給一個變量,以便可以顯示在屏幕上。 – jamjam

回答

1

你想要的話,他們將永遠是可用的。這不是async/node.js編程的工作原理。

可能的:

App.Model('users').find(function (err, users) { 
    users.forEach(function(user) { 
     myCallBack(user); 
    }); 
}); 

var myCallBack = function(user) { 
    //make usernames availible here. 
    //this will be called with every user object 
    console.log(user); 
} 

其他possibilites:EventEmitter,流量控制研究文庫(例如async)。

如果您在需要開放之前已經使用其他語言編寫代碼以採取全新的方式處理數據。

0

隨着節點JS你不這樣做:

//make usernames availible here. 

你這樣做:

App.Model('users').find(function (err, users) { 
    //usernames are available here. Pass them somewhere. Notify some subscribers that you have data. 
}); 

//The code here has executed a long time ago 
相關問題