使用 'nest new' 命令开始新项目。工作正常,直到我向其中添加实体文件。
得到以下错误:
从'typeorm'导入{实体,列,PrimaryGeneratedColumn};
^^^^^^
语法错误:不能在模块外使用导入语句
我想念什么?
将实体添加到模块:
import { Module } from '@nestjs/common';
import { BooksController } from './books.controller';
import { BooksService } from './books.service';
import { BookEntity } from './book.entity';
import { TypeOrmModule } from '@nestjs/typeorm';
@Module({
imports: [TypeOrmModule.forFeature([BookEntity])],
controllers: [BooksController],
providers: [BooksService],
})
export class BooksModule {}
Run Code Online (Sandbox Code Playgroud)
app.module.ts:
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Connection …
Run Code Online (Sandbox Code Playgroud) 我正在这里进行基本的端到端测试,目前它失败了,但首先我无法摆脱打开的手柄。
\nRan all test suites.\n\nJest has detected the following 1 open handle potentially keeping Jest from exiting:\n\n \xe2\x97\x8f TCPSERVERWRAP\n\n 40 | }\n 41 | return request(app.getHttpServer())\n > 42 | .post('/graphql')\n | ^\n 43 | .send(mutation)\n 44 | .expect(HttpStatus.OK)\n 45 | .expect((response) => {\n\n at Test.Object.<anonymous>.Test.serverAddress (../node_modules/supertest/lib/test.js:61:33)\n at new Test (../node_modules/supertest/lib/test.js:38:12)\n at Object.obj.<computed> [as post] (../node_modules/supertest/index.js:27:14)\n at Object.<anonymous> (app.e2e-spec.ts:42:8)\n
Run Code Online (Sandbox Code Playgroud)\nimport { Test, TestingModule } from '@nestjs/testing'\nimport { HttpStatus, INestApplication } from "@nestjs/common";\nimport * as request from 'supertest'\nimport { AppModule …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Nestjs 创建一个简单的测试,但收到此错误
测试函数不能同时接受“完成”回调并返回某些内容。要么使用“完成”回调,要么返回一个承诺。
返回值:Promise{}
单元测试如此简单,但是当我使用done();时出现错误
it('throws an error if a user signs up with an email that is in use', async (done) => {
fakeUsersService.find = () => Promise.resolve([{ id: 1, email: 'a', password: '1' } as User]);
try {
await service.signup('asdf@asdf.com', 'asdf');
} catch (err) {
done();
}
});
Run Code Online (Sandbox Code Playgroud) 除了200之外,我如何在nestjs中发送错误代码?我尝试在方法中注入响应对象,但没有方法发送错误。
save(@Body() body: any, @Res() response: Response): string {
console.log("posting...")
console.log(body)
return "saving " + JSON.stringify(body)
}
Run Code Online (Sandbox Code Playgroud)
上面的代码发送带有 20X 状态的正文,我想发送不同的状态代码,例如 400 或 500。
我正在尝试启动我的 nestJs 服务器,它一直给我这个错误:
UnhandledPromiseRejectionWarning:错误:您必须await server.start()
在调用server.applyMiddleware()
ApolloServer之前
我什至不确定从哪里调试,因为我对 NestJs 和 GraphQL 还是很陌生。
以前有人遇到过这种情况吗?我在嵌套构建后使用 CI/CD 运行我的代码,它给了我错误:
node_modules/@types/superagent/index.d.ts:23:10 - error TS2305: Module '"buffer"' has no exported member 'Blob'. 23 import { Blob } from "buffer";
我不知道为什么?如果您有此问题的解决方案,请分享。
我目前使用的是 6.0.4,我想使用 6.5.2。做这个的最好方式是什么?CLI 中有什么东西吗?我是否手动更新每个 @nestjs 包?
当前的依赖项是:
"@nestjs/common": "^6.0.4",
"@nestjs/core": "^6.0.4",
"@nestjs/microservices": "^6.0.4",
"@nestjs/passport": "^6.1.0",
"@nestjs/platform-express": "^6.0.4",
"@nestjs/swagger": "^3.0.2",
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用发出 POST 请求@nestjs/axios
,然后访问响应。
到目前为止,这是我的代码:
verifyResponse(captcha_response: String): Observable<AxiosResponse<any>> {
return this.httpService.post('some url', {
captcha_response
});
}
Run Code Online (Sandbox Code Playgroud)
但是,我不确定如何访问响应数据。如果我用来console.log()
查看回复,我会得到以下信息:
Observable { _subscribe: [Function (anonymous)] }
Run Code Online (Sandbox Code Playgroud)
我对 Nest.js 和 Observable 的概念都很陌生。如果有一个发出 HTTP 请求并访问响应数据的示例,我将不胜感激。
我目前在 NestJS 项目中使用 Swagger,并且启用了资源管理器:
在 main.js
const options = new DocumentBuilder()
.setTitle('My App')
.setSchemes('https')
.setDescription('My App API documentation')
.setVersion('1.0')
.build()
const document = SwaggerModule.createDocument(app, options)
SwaggerModule.setup('docs', app, document, {
customSiteTitle: 'My App documentation',
})
Run Code Online (Sandbox Code Playgroud)
有了这个,资源管理器可以访问,/docs
这是我所期望的。但我想知道是否可以向资源管理器添加任何身份验证层,因此只接受某些请求。
我想让这个资源管理器在生产中可以访问,但仅限于经过身份验证的用户。
提前致谢 :)
Nest.js 的新手,
我正在尝试实现一个简单的记录器来跟踪 HTTP 请求,例如:
:method :url :status :res[content-length] - :response-time ms
Run Code Online (Sandbox Code Playgroud)
根据我的理解,最好的地方是拦截器。但是我也使用了Guards,正如前面提到的,Guards在中间件之后但在拦截器之前被触发。
意思是,我的禁止访问没有被记录。我可以在两个不同的地方写日志部分,但不是。任何的想法?
谢谢!
我的拦截器代码:
import { Injectable, NestInterceptor, ExecutionContext, HttpException, HttpStatus } from '@nestjs/common';
import { Observable, throwError } from 'rxjs';
import { catchError, tap } from 'rxjs/operators';
@Injectable()
export class HTTPLoggingInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, call$: Observable<any>): Observable<any> {
const now = Date.now();
const request = context.switchToHttp().getRequest();
const method = request.method;
const url = …
Run Code Online (Sandbox Code Playgroud) nestjs ×10
typescript ×4
graphql ×2
javascript ×2
jestjs ×2
apollo ×1
buffer ×1
cicd ×1
openapi ×1
server ×1
supertest ×1
swagger-ui ×1
typeorm ×1
unit-testing ×1