Angular 7 - 'Property json does not exist on type Object'
Angular 7 - 'Property json does not exist on type Object'
我正在学习有关构建 MEAN 堆栈身份验证应用程序的教程,但我的 auth.server.ts 文件中的 registerUser 函数有问题。
讲师正在使用 angular 2,这在其他地方给我带来了问题,但我能够找到更新的解决方案。这个错误导致我在其他一些课程中遇到问题,我不确定如何解决它。
这是我的代码:
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()));
}
如果您需要,这里是完整的 auth.service.ts 文件:
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { map } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class AuthService {
authToken: any;
user: any;
constructor(private http: HttpClient) { }
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()));
}
}
如果您使用的是 HttpClient
而不是 Http
,那么您不需要使用管道或将其映射到 json。您可以简单地 return 来自 API 调用的响应。
registerUser(user) {
let headers = new HttpHeaders();
headers.append('Content-Type', 'application/json');
return this.http.post('http://localhost:5000/users/register', user, { headers: headers });
}
对于Angular7,建议使用HttpClient而不是Http。您不需要使用 .json() 在 HttpClient 中映射结果。您可以在下面找到示例代码:
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 });
}
我正在学习有关构建 MEAN 堆栈身份验证应用程序的教程,但我的 auth.server.ts 文件中的 registerUser 函数有问题。
讲师正在使用 angular 2,这在其他地方给我带来了问题,但我能够找到更新的解决方案。这个错误导致我在其他一些课程中遇到问题,我不确定如何解决它。
这是我的代码:
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()));
}
如果您需要,这里是完整的 auth.service.ts 文件:
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { map } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class AuthService {
authToken: any;
user: any;
constructor(private http: HttpClient) { }
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()));
}
}
如果您使用的是 HttpClient
而不是 Http
,那么您不需要使用管道或将其映射到 json。您可以简单地 return 来自 API 调用的响应。
registerUser(user) {
let headers = new HttpHeaders();
headers.append('Content-Type', 'application/json');
return this.http.post('http://localhost:5000/users/register', user, { headers: headers });
}
对于Angular7,建议使用HttpClient而不是Http。您不需要使用 .json() 在 HttpClient 中映射结果。您可以在下面找到示例代码:
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 });
}