0
votes

My array has a data. Why did this error pop up in my console:

Error in render: "TypeError: Cannot read property 'type' of undefined"

vue code inside method:

getRoomType(assign_id){
 return this.roomTypeName.find(o => o.room_id === parseInt(assign_id)).type;
},

vue html:

<span>{{ option.value }} ({{(option.assign_id=='no')? 'unassign' : getRoomType(option.assign_id) }})</span>

error:error message

1
Add error message text. Embed image. - SherylHohman
Welcome to SO. In the future, please add the text of the error message to the body of your post. It is easier for users to copy/paste, should they want to run a Google search on the message while trying to troubleshoot your code. Adding an image also ok, as an addition if you think it will be helpful. Usually it is not necessary though. Either wry, always type error messages and code as text, into the body of your post. Also, searching SO, or Google with the error message in quotes will often lead you right to the solution. For example: ` js "TypeError: cannot read property of undefined"` - SherylHohman

1 Answers

0
votes

I finally solved my problem.

Solution:( check the length of the array before returning value, to ensure that there is data to be returned. )

getRoomType(assign_id){
    return (this.roomTypeName.length > 0)? this.roomTypeName.find(element => element.room_id === parseInt(assign_id)).type :'error';
},