繁体   English   中英

我有错误澄清'属性'json'在'用户'类型上不存在。'

[英]I have error to clarify 'Property 'json' does not exist on type 'User'.'

我需要知道下面我的代码中这个错误的含义是什么。 将数据传递给 db 是 Post 方法,并且发生此错误是因为“类型 'User' 上不存在属性 'json'”。 检查下面的代码。

Angular 版本:12.2.12 节点:16.13.0 包管理器: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());
    };
  }

我需要一个解决方案!

我会尝试通过类型断言响应:

从...

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

我会做

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

尝试在管道运算符中使用 map ,如下所示。

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

也像这样从 RxJs 导入mappipe

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM