Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialyangchun li
14,456 PointsCannot understand the Parameter in function(next)
// hash password before saving to database
UserSchema.pre('save', function(next) {
var user = this;
bcrypt.hash(user.password, 10, function(err, hash) {
if (err) {
return next(err);
}
user.password = hash;
next();
});
});
Where is the parameter coming from? And where do we use it? Is the 'next' in next() the same 'next' as the parameter? what is the next()?
1 Answer
Steven Parker
229,644 PointsThe parameter "next" is a callback to your callback. As you can see from the code defined here, the parameter will be a function that will be called. But this function is itself a callback being passed to the "pre" method of "UserSchema".
So when the "pre" method invokes this callback, it will supply the other callback as the parameter "next". The code that does that is inside the "pre" method (not shown here).