我正在正确地从 Nest 6 升级项目,但我不知道如何将当前使用的 HTTP 服务器实例注入到一个类中。
以前,我一直在使用HTTP_SERVER_REFfrom@nestjs/core像这样:
@Inject(HTTP_SERVER_REF) private readonly httpServer: HttpServer
Run Code Online (Sandbox Code Playgroud)
这个常数似乎不再存在了。我有一些猴子补丁解决方案可以让我访问 HTTP 服务器,但我想知道:是否有一种新的、正确的方法来注入 HTTP 服务器?@nestjs/platform-express顺便说一下,我正在使用默认包。
大家晚上好
我正在尝试通过 TypeORM 查询存储在 sqlite 数据库上的名为“MessageEntityXREF”的表。
数据库已连接,其他表可访问。
When I try to Get on this specific table: "QueryFailedError: SQLITE_ERROR: no such table: message_entity_xref"
Any help or advice would be greatly appreciated.
Thanks
I tried to change the order in the constructor, changed the typing. However, considering the same method works for other tables, I tend to think there is an issue with this table. I queried the sqlite db for a list of all tables, it gives a list with the …
我有一个控制器(文章)。
它执行 routes: /articles,/articles/:id 就是这样。
我还需要以下路线- ,/articles/creator/:creatorId,,等等。/articles/:id/like/articles/:id/unlike/articles/:id/comment
无论我需要静态路径还是动作,它都是嵌套的并且不起作用。
我的部分解决方案 - 控制器(文章)、控制器(文章/创作者)、控制器(文章/类似)、控制器(文章/不同)。
但这是一个愚蠢的解决方案,路径和动作的概念丢失了。
有没有优雅的解决方案来解决这个问题?以及如何以最佳方式实现这一目标?
编码:
@Controller('articles')
class ArticlesController{
@Get(':articleId')
getById(@Param('articleId') articleId){}
@Post(':articleId/like)
like(@Param('articleId') articleId){}
@Get('creator/:creatorId')
getByCreator(@Param('creatorId') creatorId:string){}
}
Run Code Online (Sandbox Code Playgroud) 我想我可以说我对微服务有点菜鸟。所以,这就是我想玩它的原因。我使用了 NestJs,因为它看起来很简单
首先,我创建了一个新应用程序,nest new myservice
然后我从微服务文档中将示例main.ts和 controller.ts复制到项目中:
main.ts:
import { NestFactory } from '@nestjs/core';
import { Transport } from '@nestjs/microservices';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.createMicroservice(AppModule, {
transport: Transport.TCP,
options: { host: 'localhost', port: 3005 },
});
app.listen(() => console.log('Microservice is listening'));
}
bootstrap();
Run Code Online (Sandbox Code Playgroud)
app.module.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@Module({
imports: [],
controllers: …Run Code Online (Sandbox Code Playgroud) 我有一个 NX monorepo,其中主要包含 Angular 前端代码,我们已经添加了一个快速服务器,我正试图弄清楚如何部署它。问题是如何将基于供应商的代码与仅用于服务器应用程序的节点模块分开。
通常,对于典型的 Angular 应用程序,我们有一个包含所有 3rd 方依赖项的供应商文件,从历史上看,对于服务器应用程序,它们会有自己的 package.json,我们可以在构建时在本地安装。我假设他们是解决这个问题的一种方法,但我没有找到任何参考。我真的不想把整个节点模块文件夹放在服务器上,也不想创建一个单独的 package.json 只引用服务器代码。
任何帮助表示赞赏,谢谢
我已经在这个问题上击败了一个星期,但网上没有任何解决办法。不返回数据对象,甚至不返回“null”。我正在从工作的 REST CRUD 应用程序转换为 GraphQL。没有我想象的那么容易。
我在 Playground 中的突变:
mutation createMember($input: MemberInput!) {
createMember (input: $input) {
first_name
last_name
user_name
}
}
Run Code Online (Sandbox Code Playgroud)
以下 Playground 查询变量:(不在标题部分。)
{
"input": {
"first_name": "John",
"last_name": "Creston",
"user_name": "jc"
}
}
Run Code Online (Sandbox Code Playgroud)
模式:(查询工作正常,成员类型,TypeORM 中的一个实体,与它们和完整的 REST CRUD 一起使用。)
input MemberInput {
first_name: String!
last_name: String!
user_name: String!
}
type Mutation {
createMember(input: MemberInput!): Member!
}
type Query {
getMembers: [Member]
getMember(member_id: Int!): Member!
checkUserName(user_name: String): Member
checkEmail(email: String): Member
}
Run Code Online (Sandbox Code Playgroud)
我不知道解析器如何成为此错误消息的问题,但我将添加 Nestjs 解析器:
@Mutation('createMember')
async createMember(@Args('input') input: MemberInput): …Run Code Online (Sandbox Code Playgroud) 我一直在试图弄清楚如何对 NestJS 服务进行单元测试。所以我写了一个规范文件来测试这些使用 jest 的 NestJS 服务。
规范文件如下:
import { Test, TestingModule } from '@nestjs/testing';
import { EventsService } from './events.service';
import { getModelToken } from '@nestjs/mongoose';
import { CreateEventDto } from './dto/create-event.dto';
const event = {
_id: '53d53d2s',
name: 'Event Name',
description: 'Description of the event.',
min_team_size: 0,
max_team_size: 4,
event_price: 300,
};
describe('EventsService', () => {
let service: EventsService;
const eventModel = {
save: jest.fn().mockResolvedValue(event),
find: jest.fn().mockResolvedValue([event]),
findOne: jest.fn().mockResolvedValue(event),
findOneAndUpdate: jest.fn().mockResolvedValue(event),
deleteOne: jest.fn().mockResolvedValue(true),
};
beforeEach(async () => { …Run Code Online (Sandbox Code Playgroud) 使用 TypeORM QueryBuilder() 查询数据库时,我得到:
QueryFailedError: invalid input syntax for integer: "X"
Run Code Online (Sandbox Code Playgroud)
X 是存储在数据库中的值。
最初我的实体是类型的;
{type: 'decimal', precision: 5, scale: 2 }
value: number
Run Code Online (Sandbox Code Playgroud)
由于我已将其更改为:
{type: 'real'}
value: string
Run Code Online (Sandbox Code Playgroud)
并尝试:
'float'
value: string
Run Code Online (Sandbox Code Playgroud)
所有三种类型都会抛出相同的错误。但是,如果数据库中的值没有任何小数位 - 它工作正常。
我正在运行 Postgres v11.4、TypeORM v0.2.18 和 Nest.js v6.5.3
实体定义:
export class Entity extends BaseEntity {
@Column('float')
value: string;
}
Run Code Online (Sandbox Code Playgroud)
查询:
const current = await this.entityRepo
.createQueryBuilder('uL')
.leftJoin('uL.user', 'user')
.leftJoinAndSelect('uL.currentLevel', 'cL')
.where('user.id = :id', { id: userId })
.getOne();
Run Code Online (Sandbox Code Playgroud)
我期望返回的实体的值是正确的十进制间距。
使用 postgres 数据库,我能够连接到数据库,但是即使按照教程一步一步,在创建 user.entity.ts 文件(下面的代码)后,数据库中没有任何变化。
据我所知,最新版本的 postgres/typeorm 已正确安装。
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'
@Entity()
export class Users {
@PrimaryGeneratedColumn('uuid')
id: number
@Column({
length: 50
})
firstName: string;
}
Run Code Online (Sandbox Code Playgroud)
这是 ormconfig.json
{
"type": "postgres",
"host": "localhost",
"port": "5432",
"username": "postgres",
"password": "pw",
"database": "metabook",
"synchronise": true,
"logging": true,
"entities": ["./dist/**/*.entity{.ts,.js}"]
}
Run Code Online (Sandbox Code Playgroud)
它应该添加带有 2 列(id 和 firstName)的“用户”表。在控制台中,日志记录(在 ormconfig.json 中设置为 true)应该显示正在运行的 sql 查询以创建表,但除了成功运行应用程序(下面的输出)之外没有任何其他反应。
有谁知道我是否遗漏了什么?
我正在尝试使用 typescript 和 Nestjs 框架调试我的笑话测试。我尝试了很多命令,但似乎没有一个有效。我也尝试过NestJs typescript starter提供的这个脚本,但它不起作用。
这是命令:
”test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand"
每次运行此命令后,什么都没有发生,控制台中只会出现此消息:
我的所有测试在没有调试模式的情况下都可以正常工作。
我发现一些博客文章/教程告诉我们如何使用 typescript 和 ts-jest 调试 jest 测试,但没有一个对我有用:(
我的问题是:
nestjs ×10
node.js ×4
typescript ×4
javascript ×3
typeorm ×3
postgresql ×2
angular ×1
config ×1
graphql ×1
jestjs ×1
mongoose ×1
nrwl ×1
nrwl-nx ×1
sqlite ×1
unit-testing ×1