admin管理员组

文章数量:1431918

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 bineReducers.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 bineReducers.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
here is my console

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 bineReducers.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 bineReducers.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./facebook/immutable-js/issues/597
here is my console

Share Improve this question edited Aug 6, 2017 at 12:24 user1688401 asked Aug 6, 2017 at 11:44 user1688401user1688401 1,8719 gold badges49 silver badges88 bronze badges 2
  • If you want to map players to their id's then you should use an object instead of an array with the key being the player id and the value being an object containing the player data. Your error indicates there are null values in your players array which i can't imagine is intended. – trixn Commented Aug 6, 2017 at 12:21
  • @trixn thanks..I edited question now it give immutableJS error – user1688401 Commented Aug 6, 2017 at 12:25
Add a ment  | 

2 Answers 2

Reset to default 1

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
   }
});

if you just want to check if the array has elements then you can do something like the following....

if (players && players.length > 0) {/* Do Stuff */}

if you are concerned with specific indices within the array being null then you could do something like this....

if (players.filter(degisken => degisken !== null)
   .map((degisken) => degisken.get('id'))._tail === undefined)
   {/* Do Stuff */}

本文标签: javascriptUncaught TypeError Cannot read property 39get39 of null in map functionStack Overflow