Reputation: 79
This is my object :
const action = {
durations: ["29 minutes", "34 minutes", "2 heures 48 minutes"],
filteredCollabs: [{…}, {…}, {…}]
}
And I would like to access the values of the property "durations".
action.durations[anyIndex] returns "undefined".
That's my whole action creator (redux) :
export const searchPeople = (clientAdress, skill, collaborators) => {
const filteredCollabs = collaborators.filter(collab => collab.skills.includes(skill));
let collab;
let origin = "";
let mode = "";
let durations = [];
for (let i = 0; i < filteredCollabs.length; i++) {
collab = filteredCollabs[i];
origin = `${filteredCollabs[i].latitude},${filteredCollabs[i].longitude}`;
mode = `${filteredCollabs[i].mode}`;
axios
.get(`https://maps.googleapis.com/maps/api/distancematrix/json?origins=${origin}&destinations=${clientAdress}®ion=FR&mode=${mode}&key=${config.gmap.key}`)
.then(response => durations.push(response.data.rows[0].elements[0].duration.text))
.catch(err => console.log(err));
}
console.log("durations dans actions : ", durations);
return {
type: "SEARCH_PEOPLE",
durations,
filteredCollabs,
}
This is what my console.log returns in my browser :
durations dans actions : []
0:"29 minutes"
1:"34 minutes"
2:"2 heures 48 minutes"
length:3
__proto__:Array(0)
Thanks in advance!
Upvotes: 1
Views: 79
Reputation: 79
It was an asynchronous problem. In fact my array was empty when passed to my reducer since the response from the request was not fast enough. In my browser, however, I could see the freshly updated object. Chrome alerted me with the little info icon "value below was evaluated just now".
Thank you all for your help!
Upvotes: 0
Reputation: 316
You missed the comma after durations object
const action = {
durations: ["29 minutes", "34 minutes", "2 heures 48 minutes"],
filteredPeople: []
}
get your index result
consol.log(action.durations[0])
Upvotes: 1