Skip to content

Instantly share code, notes, and snippets.

@pratik60
Created May 2, 2014 08:20
Show Gist options
  • Select an option

  • Save pratik60/11470055 to your computer and use it in GitHub Desktop.

Select an option

Save pratik60/11470055 to your computer and use it in GitHub Desktop.
user migrate
var user_migration_error = [];
function user_migration_save(user) {
user.save(function(err) {
if (err) {
console.log(err);
console.log(user.email);
user_migration_error.push(user);
}
else {
console.log('well done ' + user.email);
}
});
}
exports.migrate = function(req, res) {
var url = 'http://intranet.xoxo.com/beta/users';
var users = [];
request(url, function(error, response, html){
if(!error){
var data = JSON.parse(response.body);
for(var i in data) {
//console.log(data[i].uid.content);
var user = new User();
user.provider = 'ldap';
user.name = data[i].name.content;
user.username = data[i].name.content;
user.email = data[i].mail.content;
user.uid = data[i].uid.content;
//Roles
if (data[i].rid) {
user.roles = data[i].rid.content.split(',');
}
....
....
....
user_migration_save(user);
}
res.jsonp(user_migration_error);
}
})
}
@pratik60
Copy link
Author

pratik60 commented May 2, 2014

I want to execute this -: res.jsonp(user_migration_error) synchronously ,basically only after the for loop is over

But it gets executed asynchronously, and the users which couldn’t be migrated are not populated on the screen. Things are working perfectly fine in the backend, just the output on the screen is wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment