typegoose: Enum not working from other files

Versions

  • NodeJS: v12.13.1
  • Typegoose(NPM): 6.2.2
  • mongoose: 5.8.9
  • mongodb: 0.0.0
  • nestjs: 6.11.1

What is the Problem?

I can’t use enum prop field

Error: Invalid type used for map!, got: "function Object() { [native code] }" (OrganizationEntity.entityType)
    at Object._buildPropMetadata (/.../node_modules/@typegoose/typegoose/src/prop.ts:224:15)
    at Object._buildSchema (/.../node_modules/@typegoose/typegoose/src/internal/schema.ts:47:7)
    at buildSchema (/.../node_modules/@typegoose/typegoose/src/typegoose.ts:170:9)
    at getModelForClass (/.../node_modules/@typegoose/typegoose/src/typegoose.ts:105:58)
    ......

Code Example

// some-type.enum.ts
export enum SomeType {
    Type1 = 'Type 1',
    Type2 = 'Type 2',
}
import { SomeType } from './some-type.enum';
export class Item {
    @prop({ enum: SomeType })
    type?: SomeType;
}

Do you know why it happenes?

The enum is imported from another file when I use enum in the same class file, it’s working!!

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 23 (10 by maintainers)

Commits related to this issue

Most upvoted comments

@hasezoey It’s seems nx enable transpile-only option by default

https://github.com/nrwl/nx/issues/2147#issuecomment-609803071

https://github.com/nrwl/nx/blob/670a5f90846bd455ef31b8f5877c6bc697346c27/packages/node/src/utils/config.ts#L43

To solve the issue I have to do this:

// webpack.config.js
module.exports = (config, context) => {
    for (const rule of config.module.rules) {
        if (rule.loader !== 'ts-loader') {
            continue;
        }

        rule.options.transpileOnly = false;
    }

    return config;
};
// angular.json
"architect": {
    "build": {
        "builder": "@nrwl/node:build",
        "options": {
                "outputPath": "dist/apps/api",
                "main": "apps/api/src/main.ts",
                "tsConfig": "apps/api/tsconfig.app.json",
                "webpackConfig": "apps/api/webpack.config.js", // <--- add this line
                "assets": ["apps/api/src/assets"]
         },

@hasezoey I had this same problem in my NestJS server using nestjs-typegoose using NX (which actually doesn’t matter since it uses ng commands) which uses Angular CLI which uses webpack to build the project. Changing

@prop({enum: CartType}) public cartType?: CartType;

To ( like you pointed out ) works for me

@prop({enum: CartType, type: String}) public cartType?: CartType;

@AliYusuf95 me too facing same issue. waiting for the solution.

This is not working:

import { SomeType } from './some-type.enum';
export class Item {
    @prop({ enum: SomeType, type: mongoose.Schema.Types.String })
    type?: SomeType;
}
Error: Invalid type used for map!, got: "function SchemaString(key, options) {
  this.enumValues = [];
  this.regExp = null;
  SchemaType.call(this, key, options, 'String');
}"

But this is working:

import { SomeType } from './some-type.enum';
export class Item {
    @prop({ enum: SomeType, type: String })
    type?: SomeType;
}