【问题标题】:Trying to make a POST request with Postman but nothing happens (NestJS and TypeORM)尝试使用 Postman 发出 POST 请求,但没有任何反应(NestJS 和 TypeORM)
【发布时间】:2020-08-01 18:47:17
【问题描述】:

我正在尝试制作一个简单的POST request,但没有任何反馈,并且新数据不会进入数据库 (Postgres)。我正在使用 NestJS 和 TypeORM。当我尝试制作GET request 时,一切都很好。我是 Postman 的新手,但我在互联网上找不到任何帮助...

邮递员 POST 请求(我也只用用户名和密码尝试过):

{
    "id": 11,
    "username": "proba11",
    "password": "proba11",
    "method": 1,
    "status": "ACTIVE"
}

带有 TypeORM 的实体模式:

@Entity({ name: "user_user" })
export class User extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ unique: true })
  username: string;

  @Column()
  password: string;

  @Column()
  method: number;

  @Column()
  status: UserStatus;

  @CreateDateColumn({ type: "timestamp" })
  createdat: number;

  @UpdateDateColumn({ type: "timestamp" })
  updatedat: number;
}

控制器:

@Post()
async create(@Res() resp: Response, @Body() user: User) {
 const ret = await this.userService.save(user);
 resp.status(ret).send();
  }

最后是服务的保存功能:

hash1(password: string): string {
    var newpass = password + pepper;
    const salt = bcrypt.genSaltSync(10);
    return bcrypt.hashSync(sha3_512(newpass, undefined), salt);
  }

async save(user: User): Promise<number> {
    try {
      user.status = UserStatus.ACTIVE;
      user.method = 1;
      user.password = this.hash1(user.password);
      await this.userRepositroy.save(user);
      return HttpStatus.OK;
    } catch (e) {
      if (e instanceof QueryFailedError) return HttpStatus.CONFLICT;
      this.logger.error(e);
      return HttpStatus.BAD_REQUEST;
    }
  }

希望大家能帮忙:)

【问题讨论】:

  • 在邮递员中显示你的标题
  • @A.khalifa 没有任何特殊的标题。我只编辑了 Body 的 raw。
  • 尝试在你的邮递员中添加这个 header content-type: application/json
  • @A.khalifa 非常感谢!
  • 不客气,我会写答案取决于我们的讨论

标签: node.js postgresql typescript nestjs typeorm


【解决方案1】:

尝试在 Postman 中添加 header 以接受 json,如下所示:

"Content-Type": "application/json"

希望对你有用

【讨论】:

    猜你喜欢
    • 2021-07-27
    • 2019-10-12
    • 2014-04-15
    • 2021-02-02
    • 2017-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-25
    相关资源
    最近更新 更多