Skip to content

When I am trying to login then showing the error message . #6959

@Arathy-sivan

Description

@Arathy-sivan
src/controllers/ctr-users.controller.ts:78:18 - error TS2345: Argument of type '{ description: string; required: boolean; content: { 'application/json': { schema: { type: string; required: string[]; properties: { login: { type: string; }; password: { type: string; minLength: number; }; }; }; }; }; }' is not assignable to parameter of type 'Partial<RequestBodyObject>'.
  Types of property 'content' are incompatible.
    Type '{ 'application/json': { schema: { type: string; required: string[]; properties: { login: { type: string; }; password: { type: string; minLength: number; }; }; }; }; }' is not assignable to type 'ContentObject'.
      Property ''application/json'' is incompatible with index signature.
        Type '{ schema: { type: string; required: string[]; properties: { login: { type: string; }; password: { type: string; minLength: number; }; }; }; }' is not assignable to type 'MediaTypeObject'.
          Types of property 'schema' are incompatible.
            Type '{ type: string; required: string[]; properties: { login: { type: string; }; password: { type: string; minLength: number; }; }; }' is not assignable to type 'ReferenceObject | SchemaObject | undefined'.
              Type '{ type: string; required: string[]; properties: { login: { type: string; }; password: { type: string; minLength: number; }; }; }' is not assignable to type 'SchemaObject'.
                Types of property 'type' are incompatible.
                  Type 'string' is not assignable to type '"string" | "number" | "boolean" | "object" | "integer" | "null" | "array" | undefined'.

78     @requestBody(CredentialsRequestBody) credentials: Credentials,

My controller is

// Uncomment these imports to begin using these cool features!

import { inject } from "@loopback/core";
import { repository } from "@loopback/repository";
import { getJsonSchemaRef, post, requestBody } from "@loopback/rest";
import { TokenServiceBindings, UserServiceBindings } from "../keys";
import { MdlUsers } from "../models";
import { Credentials, MdlUsersRepository } from "../repositories";
import { JWTService } from "../services/jwt-service";
import { MyUserService } from "../services/user-service";
import { CredentialsRequestBody } from "./specs/ctr-users-controller.specs";

// import {inject} from '@loopback/core';

export class CtrUsersController {
  constructor(
    @repository(MdlUsersRepository) public userRepository: MdlUsersRepository,
    @inject(TokenServiceBindings.TOKEN_SERVICE)
    public jwtService: JWTService,
    @inject(UserServiceBindings.USER_SERVICE)
    public userService: MyUserService,
  ) { }
  @post('users/signup', {
    responses: {
      '200': {
        description: 'NewUser',
        content: {
          'application/json':{
       // content: {
          /* schema:{
             type: 'object',
             properties: {
               password: {
                 type: 'string',
               },
             },
           },*/
          schema: getJsonSchemaRef(MdlUsers),
        },
      },
    },
  },
  })
  /* async signup(@requestBody() password:string) {
     const md5 = require('md5')
     var password = 'geeks123'
     console.log('Normal password : ', password)
     console.log('Hashed password : ', md5(password))
   }*/
  async signup(@requestBody() user: MdlUsers) {
    const md5 = require('md5')
    user.password = md5(user.password);
    const savedUser = await this.userRepository.create(user);
    //delete savedUser.password;
    return savedUser;

  }
  @post('/users/login', {
    responses: {
      '200': {
        description: 'Token',
        content: {
          'application/json': {
            schema: {
              type: 'object',
              properties: {
                token: {
                  type: 'string',
                },
              },
            },
          },
        },
      },
    },
  })
  async login(
    @requestBody(CredentialsRequestBody) credentials: Credentials,
  ): Promise<{ token: string }> {
    // ensure the user exists, and the password is correct
    const user = await this.userService.verifyCredentials(credentials);

    // convert a User object into a UserProfile object (reduced set of properties)
    const userProfile = this.userService.convertToUserProfile(user);

    // create a JSON Web Token based on the user profile
    const token = await this.jwtService.generateToken(userProfile);

    //insert generated token into the database
    const update_token = await this.userRepository.updateById(userProfile.id, { token: token })

    return { token };
  }
}

How to solve this error?

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs steps to reproduceIssues missing a small app and/or instructions for reproducing the problem

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions