I am trying to set a authenticator for my local application. Found that I am not able to access my var outside my .then() method
var authenticated = false;
Auth.findtheUser({
}).then(user => {
verified= user.name.verified;
console.log("authenticated11", verified)
}
).catch(err => console.log(err));
console.log("authenticated", verified)
the output for authenticated11 output comes as true but not the one at the end Could anyone help me out on this. New to javascript.
You cant use the result of an asynchronous function directly after it in JS. See this question: How to return the response from an asynchronous call
According to what you described in a comment in this answer. You are facing an exception related to the scope.
var authenticated = false;
Auth.findtheUser({
}).then(user => {
verified= user.name.verified;
console.log("authenticated11", verified)
}
).catch(err => console.log(err));
// This will throw an exception because verified does not exist in this scope
console.log("authenticated", verified)
You should change that to this
var authenticated = false;
var verified;
Auth.findtheUser({
}).then(user => {
verified= user.name.verified;
console.log("authenticated11", verified)
}
).catch(err => console.log(err));
// This will throw an exception because verified does not exist in this scope
console.log("authenticated", verified)