admin管理员组文章数量:1023251
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
-
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
2 Answers
Reset to default 1Generally , 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 */}
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
-
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
2 Answers
Reset to default 1Generally , 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
版权声明:本文标题:javascript - Uncaught TypeError: Cannot read property 'get' of null in map function - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745580920a2157304.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论