简体   繁体   中英

react.js foreach variable error

Why do I get record is not defined in the console? It also doesnt print any value

this.props.records.forEach(function(record){ 
      numbers.push(        
        <tr key={record.id} >
          <td>{record.title}</td>
        </tr>)
      })

Error in console

Uncaught ReferenceError: record is not definedReact.createClass.onChange @ Inline JSX script:137React.createClass.changeHandler @ Inline JSX script:82ReactClass.createClass._handleChange @ react-with-addons.js:9067executeDispatch @ react-with-addons.js:3307forEachEventDispatch @ react-with-addons.js:3295executeDispatchesInOrder @ react-with-addons.js:3316executeDispatchesAndRelease @ react-with-addons.js:2689forEachAccumulated @ react-with-addons.js:19368EventPluginHub.processEventQueue @ react-with-addons.js:2896runEventQueueInBatch @ react-with-addons.js:11161ReactEventEmitterMixin.handleTopLevel @ react-with-addons.js:11187handleTopLevelImpl @ react-with-addons.js:11273Mixin.perform @ react-with-addons.js:18340ReactDefaultBatchingStrategy.batchedUpdates @ react-with-addons.js:9613batchedUpdates @ react-with-addons.js:16573ReactEventListener.dispatchEvent @ react-with-addons.js:11367

Chances are your array contains some undefined values (eg [{id: 1}, undefined, {id: 2}] ).

console.log(this.props.records);
// or set a debugger statement to inspect the state

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM