I am new to Javascript, I am trying to invoke code on onClick event inside React container component below is my code
const onClickHandler = (e) => {
if (e != null){
const row_id = e.target.getAttribute('key');
var cur_row;
if (api_data != undefined || api_data != null){
for (cur_row=0; api_data.length-1; cur_row++){
console.log('this is api data', api_data[cur_row]['id'])
if (api_data[cur_row]['id'] == row_id){
console.log(api_data[cur_row])
break
}
}}
}}
in above code api_data is hookstate variable in which i am storing data. in console log I am able to see the data but when it comes to if condition which is right below the console.log statement I am getting error 'Cannot read property 'id' of undefined'. I searched some similar questions but not able to understand who to fix my problem. Any idea what I am missing
const onClickHandler = (e) => {
if (e != null){
const row_id = e.target.getAttribute('key');
var cur_row;
if (api_data != undefined || api_data != null){
/*
I guess you forgot to add exit statement in for loop,
because of it curr_row value was increasing way beyond
api_data.length giving you undefined output and hence that error
*/
for (cur_row=0; curr_row <= api_data.length-1; cur_row++){
console.log('this is api data', api_data[cur_row]['id'])
if (api_data[cur_row]['id'] == row_id){
console.log(api_data[cur_row])
break
}
}}
}}
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.