NestJS Mongoose - CannotDetermineTypeError:无法确定类型

问题描述 投票:0回答:1

将我的 NestJS Mongoose 库更新到以下版本后,错误不断阻止我的 NestJS 应用程序启动:

"@nestjs/common": "^7.6.18",
"@nestjs/core": "^7.6.18",
"@nestjs/mongoose": "^7.2.4",
"@nestjs/platform-express": "^7.6.18",
"dotenv": "^8.6.0",
"firebase-admin": "^9.11.0",
"mongoose": "^5.13.6",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^6.6.7"

我的代码:

@Schema()
export class ExerciseQuestion extends Document {
    @Prop()
    question: any;

    @Prop()
    solution: any;

    @Optional()
    @Prop()
    options: any[];

    @Prop()
    solutionString: string;

    @Prop()
    type: string;

    @Prop()
    difficulty: number;

    @Prop()
    tags: string[];

    @Prop()
    hint: string;
}

这是我使用

nest start
时收到的完整错误消息:

D:\Bibliothek\Dokumente\myapp\myapp_api\node_modules\@nestjs\mongoose\dist\decorators\prop.decorator.js:22
                throw new errors_1.CannotDetermineTypeError((_a = target.constructor) === null || _a === void 0 ? void 0 : _a.name, propertyKey);
                ^

CannotDetermineTypeError: Cannot determine a type for the "ExerciseQuestion.question" field (union/intersection/ambiguous type was used). Make sure your property is decorated with a "@Prop({ type: TYPE_HERE })" decorator.
    at D:\Bibliothek\Dokumente\myapp\myapp_api\node_modules\@nestjs\mongoose\dist\decorators\prop.decorator.js:22:23
    at DecorateProperty (D:\Bibliothek\Dokumente\myapp\myapp_api\node_modules\reflect-metadata\Reflect.js:553:33)
    at Object.decorate (D:\Bibliothek\Dokumente\myapp\myapp_api\node_modules\reflect-metadata\Reflect.js:123:24)
    at __decorate (D:\Bibliothek\Dokumente\myapp\myapp_api\dist\exercise\schemas\exercise.schema.js:4:92)
    at Object.<anonymous> (D:\Bibliothek\Dokumente\myapp\myapp_api\dist\exercise\schemas\exercise.schema.js:49:1)
    at Module._compile (internal/modules/cjs/loader.js:1068:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
    at Module.load (internal/modules/cjs/loader.js:933:32)
    at Function.Module._load (internal/modules/cjs/loader.js:774:14)
    at Module.require (internal/modules/cjs/loader.js:957:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (D:\Bibliothek\Dokumente\myapp\myapp_api\dist\exercise\exercise.service.js:16:27)
    at Module._compile (internal/modules/cjs/loader.js:1068:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
    at Module.load (internal/modules/cjs/loader.js:933:32)
    at Function.Module._load (internal/modules/cjs/loader.js:774:14)

为什么会出现这种情况?有什么解决办法吗?

当我尝试在 @Prop 装饰器中定义类型时,不断出现“RangeError:超出最大调用堆栈大小”错误。

我将非常感谢任何对此的建议!

mongoose nestjs mongoose-schema
1个回答
0
投票

我也遇到同样的错误。我很惊讶这个问题没有答案?

© www.soinside.com 2019 - 2024. All rights reserved.