1

I have an react app, and using redux and props to get array of objects into my component, and i am getting them. But i can't access particular property inside of one of objects that are in that array.

With this:

console.log(this.props.users)

enter image description here I get listed array with all objects inside it. But when i need to access particular object or property of that object, for example:

console.log(this.props.users[0])
console.log(this.props.users[0].name)

I am getting error:

Cannot read property '0' of undefined

But when I iterate through array with map() method i have access to it, it works. Why can't i access it normally?

9
  • 4
    please post console output for the first console statemet Commented Jan 31, 2019 at 16:28
  • 2
    What does your array of objects look like? Commented Jan 31, 2019 at 16:28
  • 1
    can you please show us the users object structure ? Commented Jan 31, 2019 at 16:29
  • 3
    What does console.log(this.props.users) show? Commented Jan 31, 2019 at 16:29
  • 2
    You are either not connecting your component to the store correctly or, if props.users is loaded asynchronously, you may be trying to access an entry in an array that has not loaded yet. Post your component code. Commented Jan 31, 2019 at 16:33

3 Answers 3

2

You are trying to access properties of this.props.users before it has loaded. Your component renders without waiting for your data to fetch. When you console.log(this.props.users) you say that you get an array, but above that, it probably logs undefined at least once when the component renders before this.props.users has loaded.

You have a couple of options. You can either do this at the very top of your render method to prevent the rest of the code in the method from executing:

if (!this.props.users) return null;

Once the data is fetched and props change, the render method will be called again.

The other option is to declare a default value, of an empty array for users in your reducer.

Sign up to request clarification or add additional context in comments.

4 Comments

i have an empty array in reducer and it didnt work with that either, but with first option that you said it works
@bobby If that's the case, you are likely overwriting the default value for users somewhere before they're fetched.
how i declare default value for a array of objects? if i just put arrayofObjects = [ ], first time when app renders arrayOfObjects.object.someProperty it will throw error that it can't read property of undefined
It is difficult to define deeply nested default values. In that case _.get from lodash is a good solution. _.get(arrayOfObjects, 'object.someProperty', 'banana') will return a default value (the 3rd arg) instead of throwing an error. lodash.com/docs/4.17.11#get
1

Might be when you are executing that line this.props.users is undefined. Check the flow where you have added console.log(this.props.users[0])

5 Comments

but at the same point i can list in console whole array with console.log(this.props.users)
@bobby Can you access this.props.users.length?
@Bergi no i can't
@bobby Can you console.log(JSON.stringify(this.props.users))? My guess is that your console log doesn't actually come from the line you thought it came
@bergi I did that and i got result in console...max have a right answer
0
const App = () => {
  
  const example = () => {
    const data =[{id:1 ,name: "Users1", description: "desc1"}, 
                {id:2 ,name: "Users2", description: "desc2"}];
    return (
      <div>
      {data.map(function(cValue, idx){
          console.log("currentValue.id:",cValue.id);
          console.log("currentValue.name:",cValue.name);
          console.log("currentValue.description:",cValue.description);
         return (<li key={idx}>name = {cValue.name} description = {cValue.description}</li>)
      })}
      </div>
      );
    }

  return(
    <p style = {{color:'white'}}>
      {example()}
    </p>
  );
}

export default App;

Comments

Your Answer

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.