Home > Net >  I have error to clarify 'Property 'json' does not exist on type 'User'.
I have error to clarify 'Property 'json' does not exist on type 'User'.

Time:11-08

I need to know what is this error meaning in my code in below. It's Post method to pass data to db and this error occured as "Property 'json' does not exist on type 'User'". Check the code in below.

Angular version: 12.2.12 Node: 16.13.0 Package Manager: npm 8.1.2

        // model.ts
        export class User{
        UserID : number
        UserName : string
        FirstName : string
        LastName : string
        Email : string
        Password : string
        ConfrimPass : string
        UserRole : string
        Authorize_building :string 
    }
    
    //component.ts
    resetForm(form? : NgForm) {
        if (form != null)
        form.reset();
        this.userService.selectUser = {
          UserID: null,
          UserName: '',
          FirstName: '',
          LastName: '',
          Email: '',
          Password: '',
          ConfrimPass: '',
          UserRole: '',
          Authorize_building: '',
        }
      }


     //service.ts
       PostUser(usr : User): Observable<User>{
       var body = JSON.stringify(usr);
       var headerOption = new Headers({ 'Content-Type' : 'application/json' });
       var requestOption = new RequestOPtions({ method: RequestMethod.Post, headers: headerOption });
    // return this.http.post('https://localhost:44339/api/Users', body, requestOption).map(x => x.json());
    
    var headersOption = ({ 'Content-Type' : 'application/json' });
    var body = JSON.stringify(usr);
    return this.http.post<User>('https://localhost:44339/api/Users', body, { headers: headersOption } ).map(x => x.json());
    };
  }

I need a solution for it!

CodePudding user response:

I'd try by type-asserting the Response:

From...

.map(x => x.json());

I would do

.map((x: Type) => x.json())

CodePudding user response:

Try using map inside a pipe operator like below.

.pipe(map((response) => response.json()))

Also import map and pipe from RxJs like this.

import { map, pipe } from 'rxjs/operators'

  • Related