I have this nested function. Am trying to pass two global variables but i have scope issue please help. I get this error running it
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,//Im trying 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'
}
});
});
}
});
}
CodePudding user response:
Assuming user
is a global variable at the start of the function save the current reference scope:
mark_paid(id: any) {
let user = user;
Swal.fire ({
CodePudding user response:
You need to make sure your 'this.user' has token value and not null. Please check your assign value of 'this.user'. Error cause due to 'this.user' which is lack of value.