I have a hard time with callback and couldn't figure out if this is actually the right way to pass a callback function as an argument. Please help me out. Below is my code snippet
Service module
jwt.signToken({userName: success.data.username, userId: success.data.userId}, secret, expiresIn, (error, token) => {
if(error) {
console.log('::::::::::Error generating token::::::::');
console.log(error);
next(returnError({message: 'Internal Server Error', status: 500}));
} else {
sendSuccessResponse({data: {token: token, user: {userName: success.data.username, userId: success.data.userId},status: 200}}, response);
}
});
Auth module
const signToken = (payload, secret, expireIn, callback) => {
jwt.sign(payload, secret, {expiresIn: expireIn}, (error, token) => callback(error, token));
};
Thanks in advance
I'm able to successfully execute the code..Reiterating look at the code how I'm handling the callback in service layer
jwt.signToken({userName: success.data.username, userId: success.data.userId}, secret, expiresIn, function(error, token) {
if(error) {
next(returnError({message: 'Internal Server Error', status: 500}));
} else {
sendSuccessResponse({
data: {
token: `${token}`,
user: {
userName: success.data.username,
userId: success.data.userId
}
},
status: 200
},response);
}
});
And my jwt module is
const signToken = (payload, secret, expireIn, callback) => {
jwt.sign(payload, secret, {expiresIn: expireIn}, (error, token) => callback(error, token));
};
This can be a simple demonstration of passing callback implementation (done in one module) to another function (in another module) as a parameter.
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.