1

I have a problem in my application.

I want to display my objects only, if they aren't null. This works pretty well for the following object:

this.val.descripiton ? [
  'descripiton', {
    label: 'label.val.descripiton',
  },
] : null,

I got a string from the API and thats why it works and if it's null - it's hidden.

But for the second object, I've a problem. Here I got an array from the API. The problem is that I always get this array, also when there are no values inside.

this.val.cities ? [
  'cities', {
    label: 'label.val.cities',
    display: cities => cities.join(', '),
  },
] && this.val.cities.length > 1 : null,

I want to check if the length is more than 1, if yes, display it. If no, hide it like the description. But I always get this error message in the console:

"TypeError: Cannot read property 'length' of undefined"

What am I doing wrong?

5
  • 5
    this.cities.length should be this.val.cities.length and check in console it's array or object. Commented Nov 17, 2020 at 9:49
  • 1
    where are you using that code? please share the whole code Commented Nov 17, 2020 at 9:54
  • @Mr.Perfectionist I updated now the question. like this, I don't get anymore the error but I never got a value. So it's always hidden Commented Nov 17, 2020 at 10:01
  • I think @Alex gave you that answer. You have to put your logic before ? Commented Nov 17, 2020 at 10:10
  • The solution from @Alex solved the error, but the cities are now always hidden - also when the array contains objects Commented Nov 17, 2020 at 10:14

1 Answer 1

2

You've misplaced the length-check:

this.val.cities && this.val.cities.length > 1 ? [
  'cities', {
    label: 'label.val.cities',
    display: cities => cities.join(', '),
  },
] : null
Sign up to request clarification or add additional context in comments.

2 Comments

with this solution, the error is solved but I it's always hidden.
Could be a problem in the template or in the remaining code. Please check your template or add it to the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.