NestJS,TypeORM,无法获取数据

NestJS, TypeORM, can't fetch data

我正在尝试开始使用使用 TypeORM 的 NestJS

我已连接到数据库。至少我认为我有,因为我遇到了很多错误,并且在对配置进行了足够的调整后,错误消失了,连接似乎成功了。

所以现在我想获取任何数据来开始。

数据库中有一个名为 RESULT_PAGE 的 table,所以我只想从中获取任何记录。这是我试过的:

结果-page.entity.ts

import { Entity, PrimaryColumn, Column } from "typeorm";

@Entity()
export class ResultPage {
    @PrimaryColumn()
    result_page_id: number;

    @Column({ length: 1 })
    approval: string;

    @Column({ length: 1})
    manually_uploaded: string;
}

结果-page.controller.ts

import { Controller, Get, Request } from '@nestjs/common';
import { ResultPageService } from './result-page.service';

@Controller('result-page')
export class ResultPageController {
    constructor(
        private resultPageService: ResultPageService
    ) { }
  @Get('get-all')
  getProfile() {
    return this.resultPageService.findAll();
  }
}

结果-page.service.ts

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ResultPage } from './result-page.entity';

@Injectable()
export class ResultPageService {
  constructor(
    @InjectRepository(ResultPage)
    private readonly resultPageRepository: Repository<ResultPage>,
  ) {}

  findAll(): Promise<ResultPage[]> {
    return this.resultPageRepository.find();
  }
}

如果我将服务编辑成如下所示:

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ResultPage } from './result-page.entity';

@Injectable()
export class ResultPageService {
  constructor(
    @InjectRepository(ResultPage)
    private readonly resultPageRepository: Repository<ResultPage>,
  ) {}

  findAll(): Promise<string> {
    return new Promise((resolve, reject) => { resolve('hello world') })
    // return this.resultPageRepository.find();
  }
}

然后我得到'hello world',所以肯定是RESULT_PAGEtable没有连接

在 AppModule 中,我正在加载这样的实体

const typeOrmModuleOptions: TypeOrmModuleOptions = {
  ...
  entities: [__dirname + '/**/*.entity{.ts,.js}'],
  synchronize: true
}

我确信我犯了一些绝对的菜鸟错误,所以如果有人能在这里帮助我,我将不胜感激。我对数据库和 api 很陌生,所以任何信息都会有很大帮助。 TIA


已解决

通过添加一个connectString解决https://github.com/typeorm/typeorm/issues/3484#issuecomment-472315370

这个问题有点混乱。我错过了错误和数据库配置。

result_page 对比 RESULT_PAGE:表名

在linux/unix表名是区分大小写的,所以你应该在注释中设置它

@Entity({name: 'RESULT_PAGE'})

如果不是这样,请提供一些细节以找出根本原因。