I have pushed some values into an array inside the @wire method and then when I try to console log those values through the renderedCallBack() function I get undefined, also if I try to access those values to create map markers I get "afterRender threw an error" error inside the same renderedCallBack() function. Would this be something related to the order of execution?
Two ways I tried console logging inside the renderedCallBack() function:
1. console.log(this.provinces);// I get the values inside proxy.
2. console.log(this.provinces[0]);//I get undefined when I access the first row of the array
Here is the minified code:
@track provinces = [];
@wire(onLoadProvinces) provStats({data}) {
if (data) {
this.provinces.push({
"key": data[i].Id,
"province": data[i].Name,
"latitude": latitude,
"longitude": longitude
});
}
}
}
renderedCallback() {
console.log(this.provinces);//this shows values inside proxy
Promise.all([
loadStyle(this, leaflet + '/leaflet.css'),
loadScript(this, leaflet + '/leaflet.js')
]).then(() => {
const el = this.template.querySelector(".map-root");
let mymap = L.map(el).setView([54.139503, -96.653471], 4);
L.tileLayer('https://api.mapbox.com/styles/, {
tileSize: 512,
zoomOffset: -1,
}).addTo(mymap);
let bcPopup = L.circle([this.provinces[0].latitude, this.provinces[0].longitude], {//this throws the afterRender error.
color: 'red',
fillColor: '#f03',
fillOpacity: 0.5,
radius: 500
}).addTo(mymap);
});
}
EDIT [Based on Ronnies answer] Thanks @Ronnie,
I put everything inside the async connectedCallBack method, which means everything that I had under the @wire method and the renderedCallBack method previously. Still when I console log(this.provinces[0]) I get undefined, but when I do console.log(this.provinces) I see the all the values in proxy.
Below is the edited code: I have edited this with my latest try, here when I run it I get an error saying provinces.map is not a function. And also I learned when we are using map we don't need to use a for loop as the map runs on every single record of the array, also here I am populating the values of latitude and longitude based on the value of a single field returned from apex call, ho would I go about doing that?
async loadProvinces() {
return await onLoadProvinces()
.catch(error => console.log("Error loading provinces: ", error));
}
async connectedCallback() {
const el = this.template.querySelector(".map-root");
const provinces = this.loadProvinces();
if (provinces) {
let latitude = 54.630088;
let longitude = -125.118077;
this.provinces = [...provinces.map(data => {
return {
"key": data[i].Id,
"province": data[i].Name,
"latitude": latitude,
"longitude": longitude
}
})]
}
await Promise.allSettled([
loadStyle(this, leaflet + '/leaflet.css'),
loadScript(this, leaflet + '/leaflet.js')
]).catch(error => console.error(error));
if (L) {
let mymap = L.map(el).setView([54.139503, -96.653471], 4);
L.tileLayer('https://api.mapbox.com/styles/v1/', {
tileSize: 512,
zoomOffset: -1,
}).addTo(mymap);
}
}
.then().catch()check the syntax of my example below. The same for promises can be done with imperative calls.