I have this code
App.Model('users').find(function (err, users) {
users.forEach(function(user) {
console.log(user.username);
});
});
//make usernames availible here.
This console logs the usernames. Instead of just logging to the console I want make use of the data.
But How can do this?
Thanks
They will never be available where you want them. This isn't how async/node.js programming works.
Possible:
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);
}
Other possibilites: EventEmitter , flow controll libraries (eg async ).
If you have written code in other languages before you need to be open to take a complete new approach how data will be handled.
With node js you don't do:
//make usernames availible here.
You do:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.