Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Computed with set but no get causes exception #5265

Closed
kmc059000 opened this issue Mar 23, 2017 · 2 comments
Closed

Computed with set but no get causes exception #5265

kmc059000 opened this issue Mar 23, 2017 · 2 comments

Comments

@kmc059000
Copy link
Contributor

Version

2.2.4

Reproduction link

http://codepen.io/anon/pen/gmKpNj?editors=1111

Steps to reproduce

Create a component with a computed which has a setter but does not have a getter. In my example, you get an exception in the console and the page does not render

What is expected?

Preferably a better error message or warning from Vue rather than an uncaught exception.

Or for backwards compatibility with Vue 2.1.8, have Vue allow a component to have a computed with no getter.

What is actually happening?

Receiving an console error and vue does not render anything.

Uncaught TypeError: Cannot read property 'toString' of undefined
at new Watcher (vue.js:2417)
at initComputed (vue.js:2743)
at initState (vue.js:2652)
at VueComponent.Vue._init (vue.js:3696)
at new VueComponent (vue.js:3862)
at createComponentInstanceForVnode (vue.js:3080)
at init (vue.js:2888)
at createComponent (vue.js:4579)
at createElm (vue.js:4522)
at createChildren (vue.js:4647)


This did not cause any exceptions in vue 2.1.8 and the component rendered and worked correctly.

This is code written by my coworker, and after upgrading vue today, this page was broken. I don't understand why it was written the way it was, and I do not think that the get is ever used, while the set is. It seems nonsensical to me to have code like this, but it exists. The problem is it took quite a while to hunt down why this was happening.

I can imagine a potential scenario where a developer misspells "get" and receives this error without any warning why, and is similarly confused on how to fix it.

@kmc059000
Copy link
Contributor Author

kmc059000 commented Mar 23, 2017

Ignore this and view the PR below instead

const getter = typeof userDef === 'function' ? userDef : userDef.get

I think that if you add the following to initComputed(), it will better handle this scenario.

//this line exists. all following lines added handles this scenario.
var getter = typeof userDef === 'function' ? userDef : userDef.get;
if(getter === undefined) {
    warn(
        `computed "${key}" is requires a "get" to be defined if a "set" is defined.` +
        `Did you define or misspell the get?`,
        vm
    )
    break;
}

I'll formalize this with a PR soon.

@yyx990803
Copy link
Member

Closing in favor of the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants