使用从数据库中检索的数据与javascript

Use data retrieved from database with javascript

本文关键字:数据 javascript 检索 数据库      更新时间:2023-09-26

我有这个代码

App.Model('users').find(function (err, users) {
        users.forEach(function(user) {
        console.log(user.username);
    });
});
//make usernames availible here.

此控制台记录用户名。我只想使用数据,而不仅仅是登录到控制台。

但是怎么能做到这一点呢?

谢谢

它们永远不会在您想要的地方可用。这不是异步/节点.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,flowcontroll库(例如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