Understanding the Code
In this code snippet, there is a function named placeDecode
that takes an HTML input element as a parameter. Inside this function, there is a promise used to convert the input value into a formatted address.
The function placeDecode
is called twice and wrapped inside a Promise.all to ensure both calls are resolved before proceeding. However, the issue arises when attempting to call another function called viewResult
, resulting in an error:
Cannot read property 'viewresult' of undefined
.
Code Overview
//calling the function twice
var startAddress = this.placeDecode1(this.searches.startSearch);
var endAddress = this.placeDecode1(this.searches.endSearch);
//using Promise.all
Promise.all([endAddress,startAddress]).then(function(values) {
this.viewResult(values);
}).catch(function(result){
console.log(result);
})
console.log('hit');
}
//method that converts input into a formatted address
private placeDecode1(input: HTMLInputElement) {
var result = new Promise(function(resolve, reject) {
var location = input.value;
var geoCode = new google.maps.Geocoder();
geoCode.geocode({
address: location
}, function(result,status){
if(status == 'OK'){
console.log(result[0].formatted_address);
resolve(result[0].formatted_address);
}
})
});
return result;
}
Encountered Problem
The main problem faced here is with the line this.viewResult(values);
, which results in the following error message:
Cannot read property 'viewResult' of undefined
Error.
Your cooperation in resolving this matter would be greatly appreciated.