欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Angular 7 - 'Property json does not exist on type Object' 有大用 有大大用

Angular 7 - 'Property json does not exist on type Object'

Ask QuestionAsked Active 9 months agoViewed 3k times1

I am following a tutorial on building a MEAN stack authentication app, and I have a problem with my registerUser function that's in my auth.server.ts file.

The instructor is using angular 2, which has caused me problems elsewhere, but I was able to find updated solutions to them. This error has caused me problems with a few other courses and I'm not sure how to fix it.

Here is my code:


  registerUser(user) {
 let headers = new HttpHeaders();
    headers.append('Content-Type', 'application/json');
 return this.http.post('http://localhost:5000/users/register', user, { headers: headers }).pipe(map(res => res.json()));
 }


Expand snippet


Here is the complete auth.service.ts file if you need it:


Show code snippet


 improve this questionasked Dec 8 '18 at 8:46Brian302 silver badges7 bronze badges

add a comment

2 Answers   正确答案

activeoldestvotes2

For Angular 7, it is adviced to use HttpClient rather then Http. You dont need to use .json() to map result in HttpClient. Below you can find example code for this:

对于angular 7,建议使用httpclient,而不是http。您不需要使用.json()在httpclient中映射结果。下面是示例代码:

(在Angular 6中,您不需要将数据映射到json中,因为默认情况下angular 6提供json。)

import { HttpClient } from '@angular/common/http';

registerUser(user) {
  let headers = new HttpHeaders();
  headers.append('Content-Type', 'application/json');
  return this.http.post('http://localhost:5000/users/register', user, { headers: headers });


}

improve this answeranswered Dec 8 '18 at 9:49Derviş Kayımbaşıoğlu17k2 gold badges25 silver badges47 bronze badges

add a comment0

If you are using HttpClient not the Http then you don't need to use pipe or need to map it to json. You can simply return the response from the API call.

  registerUser(user) {
    let headers = new HttpHeaders();
    headers.append('Content-Type', 'application/json');
    return this.http.post('http://localhost:5000/users/register', user, { headers: headers });
  }


improve this answeranswered Dec 8 '18 at 8:51Sunil Singh7,2902 gold badges9 silver badges32 bronze badges

add a comment


   

来自  https://stackoverflow.com/questions/53680951/angular-7-property-json-does-not-exist-on-type-object    


普通分类: