Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: Cannot read property 'get' of null in map function

I have findPlayerWithID function which return matched player id

function findPlayerWithID(players, id) {
  let i = 0;
  for (; i < players.count(); i++) {
        if (players.map((degisken) => degisken.get('id'))._tail === undefined) { continue; }

        if (players.map((degisken) => degisken.get('id'))._tail.array[i] === id) {
          return i;
        }
  }
  return -1;
}

But sometimes it give error in this line

 if (players.map((degisken) => degisken.get('id'))._tail === undefined) { continue; }

error is

gameStore.js:38 Uncaught TypeError: Cannot read property 'get' of null
    at gameStore.js:38
    at immutable.js:3018
    at Ue.__iterate (immutable.js:2208)
    at r.__iterateUncached (immutable.js:3017)
    at F (immutable.js:606)
    at r.T.__iterate (immutable.js:322)
    at r.toArray (immutable.js:4260)
    at new Ue (immutable.js:2067)
    at _t (immutable.js:3572)
    at Ue.map (immutable.js:4403)

I think error because of null object How can I check null in this line

if (players.map((degisken) => degisken.get('id')).




immutable.js:1317 Uncaught TypeError: Cannot read property 'merge' of null
    at immutable.js:1317
    at Ne (immutable.js:1973)
    at Ne (immutable.js:1982)
    at Ne (immutable.js:1982)
    at pe.updateIn (immutable.js:1280)
    at pe.mergeIn (immutable.js:1314)
    at gameStore.js:207
    at createReducer.js:15
    at combineReducers.js:133
    at c (createStore.js:178)

Updated with @canaan-seaton answear I change this

if (players.filter(degisken => degisken !== null).map((degisken) => degisken.get('id'))._tail.array[i] === id) {
          return i;
        }

this work but it give another error in immutablejs

Uncaught TypeError: Cannot read property 'merge' of null
    at immutable.js:1317
    at Ne (immutable.js:1973)
    at Ne (immutable.js:1982)
    at Ne (immutable.js:1982)
    at pe.updateIn (immutable.js:1280)
    at pe.mergeIn (immutable.js:1314)
    at gameStore.js:207
    at createReducer.js:15
    at combineReducers.js:133
    at c (createStore.js:178)

at this line in immutablejs

function(m ) {return typeof m.merge === 'function' ?

I searcg for that error there is some info but I don't understand what should I do https://github.com/facebook/immutable-js/issues/597
here is my console enter image description here

like image 621
user1688401 Avatar asked Mar 07 '26 10:03

user1688401


1 Answers

Generally , it is not a good practice to check for equality to undefined.

I would try to do it this way: First of all, to make sure that degisken does exists you can go with degisken && degisken.get(id)

Second, you might want to use Object's hasOwnProperty method, which will be useful in here:

players.map((degisken) => {
   const id = degisken && degisken.get('id');
   if(id && id.hasOwnProperty('_tail') && id._tail.array[i] === id){
     return i
   }
});
like image 90
matmiz Avatar answered Mar 08 '26 23:03

matmiz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!