There is a function to generate a string and return it if it is in the User table.
function generateFortToken(len) {
let rs;
rs = randomstring.generate(len);
User.findOne({where: {fort_token: rs}})
.then(result => {
console.log("hit is : ", result);
if (!result) //need to return rs. but HOW?
})
.catch(err => {
console.log("Error search for fort token : ", err.message);
});
}
This generateFortToken
is in module helper
and is called from parent function like this:
user.fort_token = helper.generateFortToken(20);
This code does not work as many online posts pointed out since findOne
returns a promise. But I am having hard time to rewrite it with callback to return the value of token generated.