-
-
Notifications
You must be signed in to change notification settings - Fork 135
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
InvalidTypeError: Type is invalid! Type is: "undefined" #326
Comments
encountering the same issue right now. |
@winterNebs there dosnt seem to be anything wrong with the provided code, please confirm the following:
|
Shoot, that's my bad guys. I had the Thanks for the quick responses |
I am seeing this same error if If I use |
@josh-feldman from what i know, this shouldnt matter, there is already an issue about shipping esm #214 and without the full error, i cannot say for sure what is happening |
@hasezoey My apologies, some detail:
Error:
User Model.ts import { prop, modelOptions, getModelForClass, DocumentType } from '@typegoose/typegoose';
@modelOptions({ schemaOptions: { timestamps: true, strict: 'throw', collection: 'user' } })
export class User {
@prop({ required: true, index: true })
userId!: number;
}
export type UserDocument = DocumentType<User>;
export const UserModel = getModelForClass(User); tsconfig: {
"compilerOptions": {
"target": "ES6",
"lib": [
"es6",
],
"downlevelIteration": true,
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"module": "es6",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"outDir": "./dist/",
"rootDir": "./src",
"preserveSymlinks": true
},
"include": [
"src",
],
"exclude": [
"node_modules",
"**/*.spec.ts",
]
} Command to start: # app.js is a simple script that connects to mongo and imports UserModel
tsc --project ./tsconfig.json && node -r esm dist/app.js |
i tried it with esm modules (after i figured out on how to do so), and had no errors, the only problem i came across was "typegoose dosnt have an default export and commonjs dosnt support named exports" (which i still dont quite understand on why) |
I am seeing this issue on Node 10 & use the For your record, this is what the above
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { prop, modelOptions, getModelForClass } from '@typegoose/typegoose';
let User = class User {
};
__decorate([
prop({ required: true, index: true }),
__metadata("design:type", Number)
], User.prototype, "userId", void 0);
User = __decorate([
modelOptions({ schemaOptions: { timestamps: true, strict: 'throw', collection: 'users' } })
], User);
export { User };
export const UserModel = getModelForClass(User);
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UserModel = exports.User = void 0;
const typegoose_1 = require("@typegoose/typegoose");
let User = class User {
};
__decorate([
typegoose_1.prop({ required: true, index: true }),
__metadata("design:type", Number)
], User.prototype, "userId", void 0);
User = __decorate([
typegoose_1.modelOptions({ schemaOptions: { timestamps: true, strict: 'throw', collection: 'users' } })
], User);
exports.User = User;
exports.UserModel = typegoose_1.getModelForClass(User); |
It also doesn't work if I try this workaround UserModel.ts: import { DocumentType } from '@typegoose/typegoose';
import * as typegoose from '@typegoose/typegoose';
// @ts-ignore
const { prop, modelOptions, getModelForClass } = typegoose.default || typegoose;
@modelOptions({ schemaOptions: { timestamps: true, strict: 'throw', collection: 'users' } })
export class User {
@prop({ required: true, index: true })
userId!: number;
}
export type UserDocument = DocumentType<User>;
export const UserModel = getModelForClass(User); compiled UserModel.js: var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import * as typegoose from '@typegoose/typegoose';
// @ts-ignore
const { prop, modelOptions, getModelForClass } = typegoose.default || typegoose;
let User = class User {
};
__decorate([
prop({ required: true, index: true }),
__metadata("design:type", Number)
], User.prototype, "userId", void 0);
User = __decorate([
modelOptions({ schemaOptions: { timestamps: true, strict: 'throw', collection: 'users' } })
], User);
export { User };
export const UserModel = getModelForClass(User); It works fine if I actually provide |
I have tested converting
in the compiled output to
and this works as well. I am thinking perhaps it has something to do with the typegoose import getting hoisted above the For now I will work around the issue with he following method. import { DocumentType } from '@typegoose/typegoose'; // Types must still be imported
/* eslint-disable @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires */
const { prop, modelOptions, getModelForClass } = require('@typegoose/typegoose'); // These must be required so they dont get hoisted
@modelOptions({ schemaOptions: { timestamps: true, strict: 'throw', collection: 'users' } })
export class User {
@prop({ required: true, index: true })
userId!: number;
}
export type UserDocument = DocumentType<User>;
export const UserModel = getModelForClass(User); |
for people using babel instead of tsc, this plugin also fixes it |
I have correct tsconfig with required metadata options enabled according to the manual and according to the comments here, I'm also using commonjs, however, I still get the same error for a case as simple as this:
|
@aderchox if your stack-trace is anything to go by, then you are using your options are:
|
When I use it in nuxt3 |
@senher please open a new issue for your case, this issue is quite old. also from what i can tell, nuxt is using esbuild, which does not support that option unless using typescript directly nuxt/nuxt#14126, at least from my quick skim |
Versions
Using the following definition for
UserClass
:I get the following unhelpful error:
As far as I can tell the type for
name
isstring
, notundefined
.Can anyone spot what I did wrong or is this error not related to this code?
I migrated from ts-mongoose since that project doesn't look like its being updated anymore, so sorry that I don't have a very minimal example.
The text was updated successfully, but these errors were encountered: