I am encountering a scope issue with my nested function while trying to pass two global variables. I need some help as I keep getting this error when running the code:
Uncaught (in promise): TypeError: Cannot read properties of undefined (reading 'user')
TypeError: Cannot read properties of undefined (reading 'user')
mark_paid(id: any) {
Swal.fire ({
title: 'Are you sure?',
text: "You must ensure that you have sent the money first before clicking this button. Providing false information or coinlocking will cause your account to be banned",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#7367F0',
cancelButtonColor: '#E42728',
confirmButtonText: 'Yes, ive sent it',
customClass: {
confirmButton: 'btn btn-primary',
cancelButton: 'btn btn-danger ml-1'
}
}).then(function(result){
if (result.value) {
const headerDict = {
'Content-Type': 'application/json',
Accept: 'application/json',
token:this.user.token,//I am attempting to access this global variable
username: this.token.username
}
const requestOptions = {
headers: new Headers(headerDict),
};
return fetch('https://some.endponint.com/markPaid/'+id,requestOptions)
.then(function (response) {
console.log(response);
if (!response.ok) {
throw new Error(response.statusText);
}else{
Swal.fire({
title: 'Trade Marked as Paid',
text: 'The seller will check on your payment and send the BTC shortly',
icon: 'success',
customClass: {
confirmButton: 'btn btn-success'
}
});
location.reload();
}
return response.json();
})
.catch(function (error) {
Swal.fire({
title: 'Ops',
text: 'An error happened please try again',
icon: 'error',
customClass: {
confirmButton: 'btn btn-success'
}
});
});
}
});
}