Vue: "TypeError: Cannot use 'in' operator to search for 'names' in bf7823uhvbon3f289ph32f

Created on 4 Jan 2018  路  8Comments  路  Source: vuejs/vue

Version

2.5.13

Reproduction link

https://jsfiddle.net/49gptnad/905/

Steps to reproduce

Just run it and open the console in a debugger

What is expected?

that the target variable is type checked for being a string before trying to perform the key in target line, to prevent a big error message

What is actually happening?

a big error message comes up


I'm going to have a look at a fix for it now so I'll post it if I figure it out

feature request good first issue

Most helpful comment

According to documentation, https://vuejs.org/v2/api/#vm-set

you are supposed to be using it in this way, i think;

this.$set(this.$data, 'testObj', 'bar');

All 8 comments

So I believe that this fix is appropriate. Because you are setting on a string, which you can do.. but really actually can't.

String variables seem a bit.............. weird

Take a look at this:
https://jsfiddle.net/49gptnad/906/

I think it's alright to simply make the code say this:

if (target.constructor !== String && key in target && !(key in Object.prototype)) {
    target[key] = val
    return val
  } else if (target.constructor == String) {
    return undefined
  }

because properties set on strings are always undefined, so technically the value is being set as the value but then becoming undefined on the next tick..

as evidenced when you open a node terminal, and type let word = 'word`` and thenword.letter = 'a'` the terminal will output 'a' for some reason, and no error, and also when you console.log(word.letter = 'a'), but, straight after when you try to access this property via it's key, it will be undefined, so, I don't see any harm in returning undefined

Feel free to submit a PR that adds a warning on development mode (+ the test ofc 馃槃 )

@posva there won't be any issues with reactivity?

$set must be used on an object anyways

Yes, you can look how warnings are implemented and tested and add a warning for the set method

will look into vue ,will get ready then start working with you guys....

Hi. I have sent a pull request about this issue: #7452

According to documentation, https://vuejs.org/v2/api/#vm-set

you are supposed to be using it in this way, i think;

this.$set(this.$data, 'testObj', 'bar');

Was this page helpful?
0 / 5 - 0 ratings

Related issues

loki0609 picture loki0609  路  3Comments

robertleeplummerjr picture robertleeplummerjr  路  3Comments

fergaldoyle picture fergaldoyle  路  3Comments

hiendv picture hiendv  路  3Comments

loki0609 picture loki0609  路  3Comments