Skip to content

Instantly share code, notes, and snippets.

@freeCodeCamp
Last active August 29, 2015 14:13
Show Gist options
  • Save freeCodeCamp/c62e3455402259a9389d to your computer and use it in GitHub Desktop.
Save freeCodeCamp/c62e3455402259a9389d to your computer and use it in GitHub Desktop.
User.find({'profile.picture': /twimg.*_normal/}, function (err, users) {
if (err) { debug('Username err: ', err); return next(err); }
for (var i = 0; i < users.length; i++) {
user = users[i];
if (user.profile.picture) {
user.profile.picture = user.profile.picture.replace('_normal', '');
user.save(function(err) {
if (err) { return next(err); }
console.log(user.profile.picture);
done(err, user);
});
}
}
process.exit(0);
});
@BerkeleyTrue
Copy link

https://gist.github.com/FreeCodeCamp/c62e3455402259a9389d#file-gistfile1-txt-L3 should say return next(err);

line 5 should check for existance first before calling a string method. i.e. thing.string = thing.string || '';

process.exit(0) here seems out of place. Are you sure you want that there?

@freeCodeCamp
Copy link
Author

I've updated to check for existence. Note that this is a standalone script we're hoping to run on the database, thus the need for an exit.

@BerkeleyTrue
Copy link

your also calling multiple an async function multiple times with a callback that ends with done. The first time done is called ends the function

@BerkeleyTrue
Copy link

I see. You should use the async module and async.each(arr, function() {//doo stuff})

@BerkeleyTrue
Copy link

and the process.exit is being called before any of the save methods can complete. It should be added as the last task in an async series

@BerkeleyTrue
Copy link

https://github.com/caolan/async#each each(arr, iterator, callback) where arr is the users array, iterator is where you call the save method, can callback is where you call process.exit(0)

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