12

I have my table view.And i have posfields that are perfecting displaying by using map function.But my problem is when i'm trying to map td inside posfields map function its throwing me the error "'headers' of undefined".

{
  this.POSFields.map(function (posFields, POSFieldsId) {
    return (
      <tr>
        <td className="posheader" key={POSFieldsId} value={posFields.POSFieldsId}
          {posFields.POSFields} </td>
    <td>
          <select className="selectpicker">
            <option value="">Select Value</option>
            {this.headers.map(function (headers) {
              return (
                <option key={headers}>{headers}</option>
              );
            })}
          </select>
        </td>
      </tr>
    )
  })
}
5
  • 3
    Use arrow functions in both map functions this.POSFields.map((posFields, POSFieldsId) => { Commented Aug 28, 2017 at 7:20
  • This seems to be undefined inside the second map. Have you tried to change the functions passed to the map methods for arrow functions? Commented Aug 28, 2017 at 7:22
  • Ok, I'm late :) Commented Aug 28, 2017 at 7:23
  • Possible duplicate of "this" is undefined inside map function Reactjs Commented Aug 28, 2017 at 7:23
  • Possible duplicate of React 'cannot read property of undefined' when using map Commented Aug 28, 2017 at 7:24

4 Answers 4

27

Like @Andrew already suggested, you should use arrowfunctions in order to be able to access this within your map. You currently loose the context of this

{
  this.POSFields.map((posFields, POSFieldsId) => {
    return (
      <tr>
        <td className="posheader" key={POSFieldsId} value={posFields.POSFieldsId}
          {posFields.POSFields} </td>
    <td>
          <select className="selectpicker">
            <option value="">Select Value</option>
            {this.headers.map((headers) => {
              return (
                <option key={headers}>{headers}</option>
              );
            })}
          </select>
        </td>
      </tr>
    )
  })
}
Sign up to request clarification or add additional context in comments.

6 Comments

just i tried using arrow fucntion as u said,Now its throwing map of undefined
Have you evaluated, that POSFields is an array? How do you declare your function? Did you bind your function?
Did you declare your function like so: myFunction = () => {..}?
My error is throwing at headers,and posfields is working fine
How does headers look like? Have you tried to console.log(this.headers)?
|
3

Bind map function to have access to this context:

{
  this.POSFields.map(function (posFields, POSFieldsId) {
    return (
      <tr>
        <td className="posheader" key={POSFieldsId} value={posFields.POSFieldsId}
          {posFields.POSFields} </td>
    <td>
          <select className="selectpicker">
            <option value="">Select Value</option>
            {this.headers.map(function (headers) {
              return (
                <option key={headers}>{headers}</option>
              );
            })}
          </select>
        </td>
      </tr>
    )
  }.bind(this))
}

Or the arrow functions this.POSFields.map((posFields, POSFieldsId) => {

Comments

0

i usually use a function inside a function to return map func

{
  this.POSFields.map(function (posFields, POSFieldsId) {
    return (
      <tr>
        <td className="posheader" key={POSFieldsId} value={posFields.POSFieldsId}
          {posFields.POSFields} </td>
    <td>
          <select className="selectpicker">
            <option value="">Select Value</option>
            {this.getData()}
          </select>
        </td>
      </tr>
    )
  })
}

getData(){
return this.headers.map(function (headers) {
              return (
                <option key={headers}>{headers}</option>
              );
            }}

Comments

0

It is the same as @Nocebo answered but with a little correction on the keys. They should be in the tag rather in the one below. If you open you browsers console you will see a warning about it:

{
  this.POSFields.map((posFields, POSFieldsId) => {
    return (
      <tr key={POSFieldsId}>
        <td className="posheader" value={posFields.POSFieldsId}
          {posFields.POSFields} </td>
    <td>
          <select className="selectpicker">
            <option value="">Select Value</option>
            {this.headers.map((headers) => {
              return (
                <option key={headers}>{headers}</option>
              );
            })}
          </select>
        </td>
      </tr>
    )
  })
}

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.