React Cannot read properties of undefined (reading ‘map’) - reactjs

I'm writing code that stores data about the walks that person do during the week.
Code: https://stackblitz.com/edit/react-wbn3ms?file=src%2FStepsForm.js
dataList works well in console but in browser there is an error:
Uncaught TypeError: Cannot read properties of undefined (reading ‘map’)
at StepsForm (StepsForm.js:84:1)
Also, when I add date it's double date.
It should work this way: https://www.loom.com/share/0159984de8474c7ab090361b3f6b68d4
Could you please help me, what do I do wrong?

setList((prevList) => {
if (index === -1) {
prevList.push(newData);
prevList
.sort((a, b) => {
return a.id - b.id;
})
.reverse();
} else {
prevList[index].distance = String(
prevList[index].distance * 1 + newData.distance * 1
);
}
return [ ...prevList ];
});
add return [...prevList] at the end you were not returning it and it was undefined and was not a array so map error

Related

Getting error "Cannot read properties of undefined (reading 'sender') " once I put equal to or less than operator

export const isSameSender = (
messages: any[],
message: any,
index: number,
user: IUser
) => {
return (
//if I put just "<" instead of "<=" in the below line, there's no error
index <= messages.length - 1 &&
messages[index + 1].sender._id === message.sender._id && //this line gives that error
messages[index].sender._id !== user._id
)
}
export const isLastMessage = (messages: any[], index: number, user: IUser) => {
return index <= messages.length - 1 && messages[index].sender._id !== user._id
}
I feel if I haven't been able to understand && properly but why does puttting <= instead of < gives the error, I am trying to check if the messages array has reached it's final index to check if it has finished looping but as soon as I use <= in this code the second code gives the error, I don't know why.
Can anyone enlighten me on what's happening? If giving any[] as a type to the messages array is causing this problem?
It's because if index is actually equal to messages.length - 1, then
messages[index + 1] is undefined because messages[messages.length - 1 + 1] which means messages[messages.length], so undefined because there is not such index in the messages array.

Discord.js | TypeError: Cannot read property 'position' of undefined

I'm working on a discord.js bot and its giving me a TypeError, but I'm not sure why.
Code:
module.exports = {
name: 'kick',
description: 'Tag a member and kick them',
execute(message) {
if(!message.member.hasPermission('KICK_MEMBERS')) {
message.channel.send("You're not an admin lmaooo")
return;
};
let mentionMember = message.mentions.members.first();
if(!mentionMember){
message.channel.send("Who do you want me to kick :smiling_imp:")
return;
}
let authorHighestRole = message.member.highestRole.position;
let mentionHighestRole = mentionMember.highestRole.position;
if(mentionHighestRole >= authorHighesrRole) {
message.channel.send("I can't kick him hes to op :tired_fac:")
return;
};
if(!mentionMember.kickable){
message.channel.send("I have no perms cant kick him lol")
};
mentionMember.kick()
.then(() => console.log(`Kicked ${member.displayName}`))
.catch(console.error);
},
};
Error: TypeError: Cannot read property 'position' of undefined
You need to specify what you're trying to code.
Node.js usually provides the line where the error has occurred.
Check if (mentionHighestRole >= authorHighesrRole) {
It probably should be authorHighestRole.
If that doesn't solve the issue, then it's probably the if statement itself, because that statement doesn't make sense to me.
EDIT: your authorHighestRole should also be let authorHighestRole = message.author.highestRole.position.

Multiple map on array React Native

Use multiple Map on array. I get error:
SyntaxError: C:\Users\Laptop15\Desktop\React Native\src\screens\Question.js: Unexpected token, expected "," (59:7)
I use function that: {PytaniePush(quizDane.quest1)}
function PytaniePush(myID) {
if(quizDane && quizDane.quest1 && myID){
myID.map((item, key)=>(
myID[key].map((item2, key2)=>(
return ({RadioPush(myID[key].item2)});
));
));
}
}
I want to console log with all elements in array and object:
quest1->array[Object(A,B), Object(A,B)]
Data look: that
I want to get:
A in 0 is ...
B in 0 is ...
A in 1 is ...
B in 1 is ...
You have an syntax error in your code. Here is right code.
function PytaniePush(myID) {
if (quizDane && quizDane.quest1 && myID) {
myID.map((item, key) =>
myID[key].map((item2, key2) => {
return RadioPush(myID[key].item2);
})
);
}
}
Next time when you will have a syntax error try to use Prettier to find error and improve format of your code.
There are some syntax errors in your code. Try this one.
function PytaniePush(myID) {
if(quizDane && quizDane.quest1 && myID){
myID.map((item, key) => myID[key].map((item2, key2) => {
return RadioPush(myID[key].item2);
} ));
}
}
Try to use an ES6 aware IDE. That way you will not stumble upon this kind of issues.

Vue.js can't copy array to another array

I'm trying to copy an array to another array and I'm getting that error:
[Vue warn]: Error in render: "TypeError: Cannot read property 'name'
of undefined"
My code:
props: {
languages: {
required: true,
},
},
data() {
return {
translations: [],
}
},
mounted() {
this.setTranslations();
},
methods: {
setTranslations() {
this.translations = this.languages.slice(0);
}
},
Same result with:
this.translations = this.languages
and:
let temp = this.languages.slice(0);
this.translations = temp;
EDIT
If I comment that line:
// this.translations = this.languages.slice(0);
Error disappears.
This also does not work:
this.languages.forEach( function (item) {
this.translations.push(item);
});
I'm getting error:
Error in mounted hook: "TypeError: Cannot read property 'translations'
of undefined"
But this works:
let temp = this.languages.slice(0);
temp.forEach( function ( lang ) {
Vue.set(lang, 'value', {});
Vue.set(lang.value, 'name', "");
Vue.set(lang.value, 'metaKeywords', "");
Vue.set(lang.value, 'metaDescription', "");
});
this.translations = temp;
Although that way my languages array became the same as translations array, which is not what I want.
What I'm doing wrong?
Actually this error comes from template. Template depends on "name" in my translations array. And it cant find it. That's why my last code works.
I don't know why console does not show error when I comment array copy line. It should show the same error.

Cannot read property 'toJS' of undefined

I have two arrays. But when one of them is null it gives the following error:
Cannot read property 'toJS' of undefined in that line
Here's the relevant call that triggers the error: {groupsGuney.toJS()}
Here's my declaration of the variables let groupsGuney, groupsKuzey;
And finally here are my two arrays. But when one of them is null it gives the error:
...
if (muso == 1) {
groupsGuney = this.props.groups
.groupBy((group, idx) => idx % maxRows)
.map((ggs, idx) => {
return this.renderGroups(ggs, idx);
}).toList().flatten(true);
}
if (muso == 2) {
groupsKuzey = this.props.groups
.groupBy((group, idx) => idx % maxRows)
.map((ggs, idx) => {
return this.renderGroups(ggs, idx);
}).toList().flatten(true);
}
var result = (
<div>
<div className={classSelector + ' discard-mini-box-area'} >
{ groupsGuney.toJS() }
</div>
<div className={classSelector + ' discard-mini-box-area'} >
{ groupsKuzey.toJS() }
</div>
</div>
);
return result;
}
}
export default DiscardMiniBoxArea;
Instead of doing:
<div>
<div className={classSelector + ' discard-mini-box-area'} >
{groupsGuney.toJS()}
</div>
....
you should do:
<div>
<div className={classSelector + ' discard-mini-box-area'} >
{groupsGuney && groupsGuney.toJS()}
</div>
....
Before calling a function on your object, you need to make sure it's there. If you're uncertain about your object having the function at all times, you will need an additional check, that makes sure toJS is there and that it's a valid function.
If that's the case, update what's inside your container to:
{groupsGuney && typeof groupsGuney.toJS === 'function' && groupsGuney.toJS()}
However, ideally, you would not render at all this specific group if what you would like to render is not there. You should move these checks to before you render your component.
My motivation here is mostly that my call .get of undefined poops itself really hard, and initializing properly all over the place helps, but doesn't catch all edge cases. I just want the data or undefined without any breakage. Specific type checking causes me to do more work later if I want it to make changes.
This looser version solves many more edge cases(most if not all extend type Iterable which has .get, and all data is eventually gotten) than a specific type check does(which usually only saves you when you try to update on the wrong type etc).
/* getValid: Checks for valid ImmutableJS type Iterable
returns valid Iterable, valid Iterable child data, or undefined
Iterable.isIterable(maybeIterable) && maybeIterable.get(['data', key], Map()), becomes
getValid(maybeIterable, ['data', key], Map())
But wait! There's more! As a result:
getValid(maybeIterable) returns the maybeIterable or undefined
and we can still say getValid(maybeIterable, null, Map()) returns the maybeIterable or Map() */
export const getValid = (maybeIterable, path, getInstead) =>
Iterable.isIterable(maybeIterable) && path
? ((typeof path === 'object' && maybeIterable.getIn(path, getInstead)) || maybeIterable.get(path, getInstead))
: Iterable.isIterable(maybeIterable) && maybeIterable || getInstead;
//Here is an untested version that a friend requested. It is slightly easier to grok.
export const getValid = (maybeIterable, path, getInstead) => {
if(valid(maybeIterable)) { // Check if it is valid
if(path) { // Check if it has a key
if(typeof path === 'object') { // Check if it is an 'array'
return maybeIterable.getIn(path, getInstead) // Get your stuff
} else {
maybeIterable.get(path, getInstead) // Get your stuff
}
} else {
return maybeIterable || getInstead; // No key? just return the valid Iterable
}
} else {
return undefined; // Not valid, return undefined, perhaps should return false here
}
}
Just give me what I am asking for or tell me no. Don't explode. I believe underscore does something similar also.

Resources