question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: Issues in constructor

See original GitHub issue

Issue type:

[ ] question [x ] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [x ] postgres [ ] sqlite [ ] sqljs [ ] websql

TypeORM version:

[x ] latest [ ] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

  1. Clone this repo https://github.com/cedrickmandocdoc/typeorm-bug
  2. Create PG Database with name test, edit what roles you will used
  3. Install packages
  4. Compile run tsc
  5. Run index.js Result:
  6. You will notice that there is a TypeError.

I guess this is because of the constructor. I believe there is workaround here, but I want to preserve also the types and have strictnullchecks set to true. Though if I edit the constructor parameters and isolate it one by one. It will works. I just want to know why this sample repo didn’t work. I guess this is a bug.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

9reactions
pleerockcommented, Feb 15, 2018

You have two issues. create does not save entity in the database and it does not return you a promise, and it does not accepts constructor arguments, you are using it completely wrong (I guess you are confusing methods of sequelize library, create in typeorm serves different purpose). Your correct code for saving entity is:

  await userRepository.save(new User({
    firstName: 'test',
    lastName: 'test',
    age: 1
  }));

Second, your entity constructor arguments must be optional, its a requirement, since when loading entities from the database ORM creates instances of the classes, and it does not aware of your constructor arguments and what it shall provide there. How it should be in your code:


  public constructor(data?: User) {
    if (data) {
      this.firstName = data.firstName;
      this.lastName = data.lastName;
      this.age = data.age;
    }
  }
7reactions
nitzantomercommented, Mar 16, 2018

That’s exactly what this feature is created for, so I think it’s using it wisely. There are no “hacks” here, in fact, your solution is the hack as it actually translates into the javascript output. My solution keeps the js code clean and simple.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: "x" is not a constructor - JavaScript - MDN Web Docs
The JavaScript exception "is not a constructor" occurs when there was an attempt to use an object or a variable as a constructor,...
Read more >
Resolving TypeError: "X" is Not a Constructor in JavaScript
JavaScript "TypeError: "x" is not a constructor" errors occur when invalid objects or a variable is erroneously used as a constructor.
Read more >
TypeError() constructor - JavaScript - UDN Web Docs
The TypeError() constructor creates a new error when an operation could not be performed, typically (but not exclusively) when a value is not...
Read more >
Javascript "Not a Constructor" Exception while creating objects
In chrome, at least, there seem to be several variations of non-function/non-ctor related messages. Your example gives "TypeError: object is not a function." ......
Read more >
TypeError: "x" is not a constructor - CodeProject Reference
Invalid cases ; var Car = 1 ; new Car(); // ; new Math(); // ; new Symbol(); // ; TypeError: Symbol is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found