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'type'of object'#<Banner>'` #1458

Closed
qualc opened this issue Jan 16, 2018 · 9 comments · Fixed by babel/babel#7429
Closed

Cannot assign to read only property'type'of object'#<Banner>'` #1458

qualc opened this issue Jan 16, 2018 · 9 comments · Fixed by babel/babel#7429

Comments

@qualc
Copy link

qualc commented Jan 16, 2018

In the JavaScript grammar. After an instance of the @entity () modified new object, the attribute value of the instance can not be changed
TypeError: Cannot assign to read only property'type'of object'#<Banner>'
I try to add descriptor.writable = true in Column and so on.

function Column(typeOrOptions, options) {
    return function(object, propertyName, descriptor) {
        descriptor.writable = true;
    };
}

Did I make a mistake?

@pleerock
Copy link
Member

Sorry, but I do not understand what you mean and what you are trying to do

@qualc
Copy link
Author

qualc commented Jan 17, 2018

Example:
/entity/banner.js

import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
@Entity('banner')
export class Banner {
    @PrimaryGeneratedColumn()
    id;
    @Column()
    type;
}

app.js

import "reflect-metadata";
import { Banner } from './entity/banner';
typeorm.createConnection({
     ...,
   entities: ['./entity/*.js'  ]
}).then(async function(connection) {
    let banner = new Banner();
    banner.type = 1;  // **TypeError: Cannot assign to read only property 'type' of object '#<Banner>'**
}).catch(e => {
    console.log(e)
});

@pleerock
Copy link
Member

Looks like an issue of the transpiler plugin you are using. Does using descriptor.writable = true in decorators solve the problem?

@qualc
Copy link
Author

qualc commented Jan 17, 2018

Yes, it does.

@pleerock
Copy link
Member

okay, are you able to create a pull request for this change and make sure its not breaking anything else in typescript infrastructure?

@qualc
Copy link
Author

qualc commented Jan 21, 2018

Emm,I need time to test it.I'll give you a reply later.

@yhpark
Copy link
Contributor

yhpark commented Feb 15, 2018

I also went through this issue. Apparently the babel class-properties proposal plugin in loose mode transpiles class properties without pre-assigned value non-writable. Not sure if this is a bug or intended..

Right now one solution is to assign any default values as below (undefined works as well):

import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
@Entity('banner')
export class Banner {
    @PrimaryGeneratedColumn()
    id = undefined;
    @Column()
    type = 1;
}

@pleerock
Copy link
Member

After certain tests I found that it does not work in typescript. I think problem is in babel plugin and it should be resolved there. I remember I was already reporting to them this issue year ago. Closing.

@qualc
Copy link
Author

qualc commented Feb 21, 2018

Yes, I also found that the porblem was due to the inconsistency of bable plugin and typescript to the interpretation of decorator.

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

Successfully merging a pull request may close this issue.

3 participants