-
Describe the problem(描述问题)在 这个问题是我将
如果换回之前的这个启动命令就会是正常的:
以下是代码片段 // log.service.ts
import { Provide, Inject } from "@midwayjs/core";
import { InjectEntityModel } from "@midwayjs/typegoose";
import { ReturnModelType } from "@typegoose/typegoose";
import { LogEntity } from "@/entity/log.entity";
@Provide()
export class LogService {
@InjectEntityModel(LogEntity )
private dbLog: ReturnModelType<typeof LogEntity >;
public async add(log: LogAdminOperationEntity) {
return await this.dbLog.create(log); // 在被调用时此处的 this.dbLog 会是 undefined
}
} // log.middleware.ts
import { Middleware, IMiddleware } from "@midwayjs/core";
import { NextFunction, Context } from "@midwayjs/koa";
import { LogService } from "@/service/log.service";
@Middleware()
export class LogMiddleware implements IMiddleware<Context, NextFunction> {
resolve() {
return async (ctx: Context, next: NextFunction) => {
const logService = await ctx.requestContext.getAsync<LogService>(LogService);
await logService.add({
// xxxx
});
}
}
} Midway Versions(Midway 版本)
|
Beta Was this translation helpful? Give feedback.
Answered by
pecasha
Apr 15, 2024
Replies: 2 comments 2 replies
-
mwtsc 是编译功能,不能启动项目吧 |
Beta Was this translation helpful? Give feedback.
2 replies
-
我找到原因了,是数据源匹配的问题, entities: [
"**/*.entity.ts"
]
entities: [
"**/*.entity.{j,t}s"
] |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
pecasha
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
我找到原因了,是数据源匹配的问题,
src/config
配置文件中的mongoose.dataSource.default.entities
我原先是配置的:mwtsc
是走编译的方式,这样会导致编译后找不到entity
文件,只需要把配置改为这样就可以了: