0

I'm expecting to display a computed getter property, but VSC gives me the error "Expected to return a value in 'features' computed property." Even though I'm returnng a true value in getter in code.

// Doesn't work
computed: {
    features() {
      if(this.$store.getters.features) {        // feature is definately true by itsels (returns true)
        return this.$store.getters.features   // error comes here
      }
    }
  }
  
  // Works fine
computed: {
    features() {
        return this.$store.getters.features 
      }
    }
  }

1
  • The way it's supposed to be written depends on the expected "features" value. You didn't explain it. Commented Jul 22, 2023 at 15:51

2 Answers 2

0

You should handle the else case :

computed: {
    features() {
      if(this.$store.getters.features) {       
        return this.$store.getters.features   
      } else{
       return false;
      }
    }
  

but it's recommended to use the second syntax.

Sign up to request clarification or add additional context in comments.

Comments

0

In a computed property, you should always ensure that a value is returned, no matter what condition is met.

In your computed property, you have an if statement that checks if this.$store.getters.features is truthy, and if it is, it returns the value. However, if the condition is not met (if this.$store.getters.features is falsy), the function does not return anything, which is the cause of the error.

Example :

computed: {
  features() {
    if (this.$store.getters.features) {
      return this.$store.getters.features;
    } else {
      return null; // You can use any return value here as per requirement
    }
  }
}

Comments

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.