1

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}&region=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!

asked Aug 19, 2018 at 10:07
5
  • If there are no syntax / console errors, it should work. Commented Aug 19, 2018 at 10:09
  • 2
    Probably because anyIndex is not 0, 1, 2 ? And you are missing a comma to seperate the properties. Commented Aug 19, 2018 at 10:09
  • 1
    What is anyIndex? Please try using the debugging capabilities of your browser. Commented Aug 19, 2018 at 10:09
  • The missing comma suggests that you don’t actually have this fixed object. Is it an AJAX response? If so, show how you’re fetching the data. Commented Aug 19, 2018 at 10:10
  • anyIndex is 0, 1, 2. I forgot the comma here, but it is present in my code. It's an action passed to my reducer in redux. No error in the console. Commented Aug 19, 2018 at 10:36

2 Answers 2

1

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])
answered Aug 19, 2018 at 10:18

2 Comments

Oops, I edited it (just forgot here because I typed manually). But it's still not working and returns undefined.
It that case you should do just durations[index] becoz you use .then(response => durations.push(response.data.rows[0].elements[0].duration.text)); In this case durations is the new array you just created action is not used there Mark as answer if the answer is correct
0

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!

answered Aug 19, 2018 at 14:41

1 Comment

Glad you came right! Well done and thanks for informing us.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.