-
Notifications
You must be signed in to change notification settings - Fork 136
TypeError: Class constructor Typegoose cannot be invoked without 'new' #60
Comments
Hi @anvlkv. export const ItemModel = new Item().getModelForClass(Item);
export class ItemsService {
private itemModel: InstanceType<Item>;
constructor(
) {
this.itemModel = new ItemModel({ /* here you can put some field to inizialize */ });
}
} |
Hi @pgsfredda, Thanks for looking into my problem I tried your suggestion, it just shifted the error to where I I've got this repo with reproduction |
Hi @anvlkv import { prop, Typegoose } from 'typegoose';
export class Item extends Typegoose {
@prop()
name: string;
@prop()
description: string;
}
export const ItemModel = new Item().getModelForClass(Item); //Here is the correction. The type of ItemModel is inferred
let trial = new ItemModel({name:'some', description:'some'});
console.log(trial);
// prints:
// { name: 'some',
// description: 'some',
// _id: 59e65dcf24b047492f48e13f } By the way, no monogoose Model is needed. ItemModel represents the schema and the collection of data with the two fields 'name' and 'description' and all the statics, the methods and the plugins. function( item: InstanceType<Item>) { ... } |
@pgsfredda Hmmm, Still got the same issue. If I'm not declaring type like According to this microsoft/TypeScript#5711 solution is to declare it. Since in this example I have it all in one file I can even ignore export and just do |
@anvlkv The pattern for the 'model file' is: import { Typegoose, prop } from 'typegoose';
export class Item exrends Typegoose {
@prop() field: simple type of field (string, number, ...)
...
};
export const ItemModel = new Item().getModelForClass(Item); Then you can use ItemModel to active find, update and create methods in the other files. Check also your grunt and tsconfig files for the options about decorations (see the README.md file). |
I had the same error as the initial post It was caused by a tsconfig.json setting (target: "es5"). Setting target to "es6" fixed the errors. I guess es5 functions are not a direct replacement for class inheritance in this case. Hope this catches the eye of anyone with a similar issue. On my end it was mostly just a copy+paste problem since I use typescript with web projects usually and using es6 there isn't really great for compatibility. |
Thanks for trying to resolve the issue @pgsfredda and @derekhawker . So if I undestand this right: Typegoose will not work with TypeScript projects targeted to "es5" right, only es6/es2015 ? @anvlkv can you try out your code with target set to es6 and post us the results? If that is the case I'll have to add this note to the documentation (or try to find a way to make Typegoose es5 target compatible) |
Cool! Thank you guys! That totally helped. I don't have any need to use other targets. update: or may be I actually do)) |
Sorry recovering this issue but is Typegoose now ES5 compatible? I still get this error when I use es5 and I need es5 as a target. |
I am facing this issue as well and I cannot use ES6 as target in my project. Is there any way I can make this work with ES5? |
Are there any plans for Typegoose be compatible with target ES5? |
I have forked this package and built for es5. Here is the link.
https://www.npmjs.com/package/typegoose-es5
…On Sun, Dec 2, 2018 at 8:26 AM Gabriel Rocha de Oliveira < ***@***.***> wrote:
Are there any plans for Typegoose be compatible with target ES5?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#60 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ARxbBD_DroBrgTuQJsxiAlvcWIFxbldMks5u0_EKgaJpZM4P53RA>
.
|
It's a real issue, as Angular's test (spec) files cannot be run properly, if I try to use a model that is based on Typegoose. Will you take over ES5 compatibility from @singhsoldier13 in future?? |
Hi,
I'm for some reason getting this error "TypeError: Class constructor Typegoose cannot be invoked without 'new'"
I have a class like this:
I also have service which uses the class like this:
As TS compiles:
The strangest part is that my src/models/item.ts is only 33 lines and problem supposed to be at line 40.
The text was updated successfully, but these errors were encountered: