So I have a use case of fetching emails of people logged in using the below code and pass it to the backend DB. Is there a way to store the output of the below code (expected scenario shown in subsequent code) in a separate variable and use it later in my code? Not sure if this is a possible scenario.
Note: I am able to print the email in the console, but not sure how to store it in a variable as shown in my second code snippit.
Auth.currentSession()
.then(data => {
let idToken = data.getIdToken();
let email = idToken.payload.email;
console.log(email);
})
.catch(err => console.log(err));
Expectation:
var fetch = Auth.currentSession()
.then(data => {
let idToken = data.getIdToken();
let email = idToken.payload.email;
console.log(email);
})
.catch(err => console.log(err));
async function addData() {
const data = {
body: {
account_id: formState.account_id,
builder: fetch,
},
};
console.log(data);
const apiData = await API.post("testapi", "/test", data);
console.log({ apiData });
alert("Request Submitted");
}
@t_killah - Thank you, I was later able to resolve this using window.email.
Auth.currentSession()
.then((data) => {
let idToken = data.getIdToken();
window.email = idToken.payload.email;
})
.catch((err) => console.log(err));
async function addData() {
const data = {
body: {
builder: window.email,
},
};
You can store it in context, and then your data will be available globally. https://reactjs.org/docs/hooks-reference.html#usecontext