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

Cannot assign to read only property of object #57

Open
pleerock opened this issue Jan 27, 2017 · 10 comments · Fixed by babel/babel#7429
Open

Cannot assign to read only property of object #57

pleerock opened this issue Jan 27, 2017 · 10 comments · Fixed by babel/babel#7429

Comments

@pleerock
Copy link

pleerock commented Jan 27, 2017

I have such error: Cannot assign to read only property 'name' of object '#<Category>' when I use non initialized properties decorated with decorators and then trying to assign values to these properties.

import {Entity, PrimaryColumn, Column} from "typeorm";

@Entity()
export class Category {

    @PrimaryColumn("int", { generated: true })
    id;

    @Column("string")
    name;

}

Solution is to do it this way:

import {Entity, PrimaryColumn, Column} from "typeorm";

@Entity()
export class Category {

    @PrimaryColumn("int", { generated: true })
    id = undefined;

    @Column("string")
    name = "";

}

Which is ugly. I guess the reason is that it does not make descriptor writable. Is there any better solution for this problem?

@loganfsmyth
Copy link
Owner

This seems like an issue with the typeorm decorators, rather than an issue with this module?

@pleerock
Copy link
Author

pleerock commented Jan 27, 2017

No, I think its not. Simple example of reproduction without typeorm:

function dec(id) {
    return (target, property, descriptor) => console.log("executed", id);
}

export class Category {

    @dec()
    id = 0;

    @dec()
    name;

}

const category = new Category();
category.id = 1;
category.name = "hello";

produces:

/Users/pleerock/www/opensource/typeorm/babel-example/dist/index.js:79
category.name = "hello";
              ^

TypeError: Cannot assign to read only property 'name' of object '#<Category>'
    at Object.<anonymous> (/Users/pleerock/www/opensource/typeorm/babel-example/dist/index.js:79:15)

Note that category.id = 1 worked because id was initialized in the class.

@loganfsmyth
Copy link
Owner

loganfsmyth commented Jan 27, 2017

Would you be able to put together a simple example repo? Dropping that into a repo with .babelrc

"transform-class-properties",
"transform-decorators-legacy",

logs

executed undefined
executed undefined

for me, so something else may be interfering?

@pleerock
Copy link
Author

why do you put "transform-class-properties" before "transform-decorators-legacy"?

@pleerock
Copy link
Author

Here is my configuration:

{
  "presets": [
    "es2015"
  ],
  "plugins": [
    "transform-decorators-legacy",
    "transform-class-properties"
  ]
}

@loganfsmyth
Copy link
Owner

You are right, I tried both to make sure and then copy-pasted the wrong one.

I am able to reproduce with es2015. Thanks for the info.

@hwaterke
Copy link

hwaterke commented Feb 3, 2017

The problem originates from this check: index.js (LINE 54)

I'm not familiar enough with the code to be sure if it can be safely removed.
Any opinion?

@loganfsmyth
Copy link
Owner

Yeah that's definitely the line. That was carried over from Babel 5.x here: https://github.com/babel/babel/blob/5.x/packages/babel/src/transformation/templates/helper-create-decorated-class.js#L15 and the same bug reproduces in Babel 5 too.

@SPAHI4
Copy link

SPAHI4 commented Feb 16, 2017

Any solutions?

@chbdetta
Copy link

chbdetta commented Dec 5, 2017

Is this solved?

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

Successfully merging a pull request may close this issue.

5 participants