[英]javascript save function usage
I am learning about building a new user login app and there is a line of code the makes me quite confused and Google did not show me any enlightening result. 我正在学习构建一个新的用户登录应用程序,其中有一行代码使我感到困惑,而Google并没有给我任何启发性的结果。
module.exports.createUser = function(newUser, callback){ bcrypt.genSalt(10, function(err, salt) { bcrypt.hash(newUser.password, salt, function(err, hash) { newUser.password = hash; newUser.save(callback); }); }); }
It is quite straightforward: The new user module is trying to crypt the password and runs the callback(It is my understanding). 这很简单:新的用户模块正在尝试加密密码并运行回调(这是我的理解)。 But this line here:
但是这行在这里:
newUser.save(callback);
It is very confusing to me and would anyone explain what it does here? 这对我来说很混乱,有人可以在这里解释吗? Thank you
谢谢
You pass a callback function as a parameter to the outer most function. 您将回调函数作为参数传递给最外部的函数。 And when the genSalt- and hash-functions are done, you pass the same callback function as a parameter to
newUser.save()
. 当genSalt和hash函数完成后,您会将相同的回调函数作为参数传递给
newUser.save()
。 It is a function that (probably) will be executed inside newUser.save()
. 这个函数(可能)将在
newUser.save()
内部执行。
Sometimes using callbacks are a way to say that some code has completed, and then run a new function. 有时使用回调是一种方法,可以说某些代码已完成,然后运行新函数。 Useful for async funcions.
对于异步功能很有用。
A simpler example: 一个简单的例子:
serveCake = function(){ // this is my callback function
// serve the cake
}
createCake = function(ingredients, callback){ // pass serveCake as a callback here.
// Mix the ingredients and put the cake in the oven
// When the cake is finished baking, serve it
callback(); // call the callback function.
}
const ingredients = ['suger', 'butter'];
createCake(ingredients,serveCake);
The same happens with your code, but the callback function will (probably) be executed inside the newUser.save()
-method. 您的代码也会发生同样的情况,但是回调函数(可能)将在
newUser.save()
方法内部执行。
Mongoose save method expects an optional callback function as an argument ( Reference Doc ). 猫鼬保存方法需要一个可选的回调函数作为参数( Reference Doc )。 So when you are calling
newUser.save(callback);
因此,当您调用
newUser.save(callback);
you are passing the same callback function that you got in the createUser(newUser, callback);
您正在传递与
createUser(newUser, callback);
相同的回调函数createUser(newUser, callback);
so whichever method will call this method ie suppose in following code callIt() function is calling your code. 因此,无论哪种方法都将调用此方法,即假设在下面的代码中callIt()函数正在调用您的代码。
module.exports.createUser = (newUser, callback) => {
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(newUser.password, salt, (err, hash) => {
newUser.password = hash;
newUser.save(callback);
});
});
};
function callIt() {
module.exports.createUser({ name: 'ridham' }, function (err, user) {
if (err) {
console.log(error);
} else {
console.log(user);
}
});
}
callIt();
so here you are giving function (err, user) {...}
function as an argument which can be called by invoked function to pass back the access to invoking the function(handling async nature of JS). 所以在这里,您将
function (err, user) {...}
用作参数,被调用的函数可以调用该参数,以将对函数的访问权传递回去(处理JS的异步特性)。 and your invoke function again invoking another function passing the same callback reference so basically, you are chaining the same callback. 并且您的invoke函数再次调用传递相同回调引用的另一个函数,因此基本上,您正在链接同一回调。
Callback Explanations can be found here and here . 回调说明可以在此处和此处找到。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.