簡體   English   中英

使用從JavaScript檢索數據庫中的數據

[英]Use data retrieved from database with javascript

我有這個代碼

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

//make usernames availible here.

該控制台記錄用戶名。 我不僅要記錄到控制台,還想利用數據。

但是該怎么辦呢?

謝謝

它們將永遠不會在您需要它們的地方可用。 這不是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);
}

其他可能性: EventEmitter ,流控制庫(例如async )。

如果您之前已經用其他語言編寫過代碼,則需要開放使用一種全新的方法來處理數據。

使用node 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM