skip to Main Content

**Entity code **


   import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';

@Entity({ name: 'app_user' })
export class User {
  @PrimaryGeneratedColumn({ name: 'user_id' })
  userId: number;

  @Column({ name: 'username', type: 'varchar', length: 255, unique: true })
  username: string;

  @Column({ name: 'user_password', type: 'varchar', length: 255 })
  userPassword: string;

  @Column({ name: 'user_role', type: 'varchar', length: 50 })
  userRole: string;
}

app-service

import { MessageCount } from './entities/message-count';
import { DataSource } from 'typeorm';
import { User } from './entities/app-user';

const logger = new Logger('AppService');

@Injectable()
export class AppService {
constructor(private readonly db: DataSource) {}
async appendUser(user: User) {
  // Assuming you have necessary validation or additional logic
  return this.appendUserToDB(user).catch((err) => {
    // Retry in case of an error
    logger.error(err);
    return this.appendUserToDB(user);
  });
}

private appendUserToDB(user: User) {
  return this.db.getRepository(User)
      .save(user)
      .then(() => {
        logger.verbose('User added successfully.');
      });
  
}

getHello(): string {
  return 'Hello World!';
}
}

Controller.ts

import { AppService } from './app.service';
import { MessageCount } from './entities/message-count';
import { User } from './entities/app-user';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  getHello(): string {
    return this.appService.getHello();
  }
@Post('add-user') // Adjust the route as needed
  async addUser(@Body() user: User) {
    try {
      await this.appService.appendUser(user);
      return { message: 'User added successfully' };
    } catch (error) {
      return { message: 'Error adding user', error: error.message };
    }
  }

app-module.ts

import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigManager } from './app.config';
import { DataSource } from 'typeorm';

const appConfigMgr = new ConfigManager();
const appDataSource = new DataSource(appConfigMgr.getConfigObject().dbConfig);

@Module({
  imports: [],
  controllers: [AppController],
  providers: [
    AppService,
    { provide: ConfigManager, useValue: appConfigMgr },
    { provide: DataSource, useValue: appDataSource },
  ],
})
export class AppModule {}

When making a POST request in Postman:

http://localhost:3000/add-user

With Body request:
{
"username": "admin",
"userPassword": "admin",
"userRole": "admin"
}

I’m getting the issue as:

{
"statusCode": 404,
"message": "Cannot POST /add-user",
"error": "Not Found"
}

I get error this while making a POST request in Postman
How can I fix the error:

2

Answers


  1. you are requesting wrong the url, the url should be:

    URL + controller name + path (here the path is add-user)

    http://localhost:3000/users/add-user
    
    {
        "message": "Cannot PUT /app-user",
        "error": "Not Found",
        "statusCode": 404
    }
    

    that means app-user doesn’t have your controller

    Login or Signup to reply.
  2. In postman you’re requested using PUT method and in controller you have define POST method.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search