I am woking on NodeJS project. I have defined a variable inside the function scope. The variable is assigned in the inner functions to some value. But, with the scope of outer function, the variable value is undefined.
Below is the code block,
getOTP(to,1).then((docs) => {
if(!Array.isArray(docs) || !docs.length){
otp = createOTP(config.OTP.otp_secret);//OTP is set here
}else{
if(!isOtpExpired(docs[0].created)){
otp = docs[0].otp.otp_number;
}else{
updateOtp(docs[0]._id,3).then((response)=>{
if(response.result.nModified == 1 && response.result.ok == 1 ){
console.log("Entered");
otp = createOTP(config.OTP.otp_secret);
}
},(error)=>{
console.error('The promise was rejected', error, error.stack);
});
}
}
},(error) => {
console.error('The promise was rejected', error, error.stack);
});
console.log('OTP',otp);//OTP is undefined
Please help me, how can I access the OTP value in the outer function. Any help will be appreciated.