属性 'success' 在类型 'Object' 上不存在
Property 'success' does not exist on type 'Object'
user.js
router.post('/register', (req, res) => {
let newUser = new User({
name: req.body.name,
email: req.body.email,
username: req.body.username,
password: req.body.password
});
User.addUser(newUser, (err, user) => {
if (err) {
res.json({ "success": false, msg: 'Failed to register User' })
} else {
res.json({ "success": true, msg: "User Added" })
}
})
});
authservice.ts
import { catchError,map } from 'rxjs/operators';
import { Injectable } from '@angular/core';
import {HttpClient,HttpHeaders,HttpErrorResponse} from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class AuthService {
constructor(private http:HttpClient) { }
registerUser(user){
let headers = new HttpHeaders();
headers.append('content-type','application/json');
return this.http.post('http://localhost:8080/users/register',user,
{headers:headers})
}
}
register.component.ts
import { ValidateService } from './../../services/validate.service';
import { Component, OnInit } from '@angular/core';
import { FlashMessagesService } from 'angular2-flash-messages';
import {AuthService} from '../../services/auth/auth.service';
import {Router } from '@angular/router';
@Component({
selector: 'app-register',
templateUrl: './register.component.html',
styleUrls: ['./register.component.css']
})
export class RegisterComponent implements OnInit {
name:String;
username:String;
email:String;
password:String;
constructor(private _validate:ValidateService,
private _flashMessagesService: FlashMessagesService,
private _authservice:AuthService,
private _router:Router) { }
ngOnInit() {
}
onRegisterSubmit(){
const user={
name:this.name,
username:this.username,
email:this.email,
password:this.password,
}
this._authservice.registerUser(user)
.subscribe(data =>{
console.log(data);
**if(data.success){**
this._flashMessagesService.show('Registration Succesfull ! ',{cssClass:'alert-success',timeout:3000})
this._router.navigate(['/login']);
}else{
this._flashMessagesService.show('Oops Something went wrong! ',{cssClass:'alert-danger',timeout:3000})
this._router.navigate(['/register'])
}
})
}
}
错误
src/app/components/register/register.component.ts(49,17) 中的错误:错误 TS2339:属性 'success' 在类型 [=43 上不存在=].
数据提交成功,甚至 angular 也成功重定向到下一个组件,但这会出错。在register.component.ts while subscribing the 属性 success of the returned object in if statement if(data.success)
您可以对您的回复进行类型检查以避免此类错误。
创建一个 RegisterResponse
class 来包含您的 HTTP 响应的结构。
export class RegisterResponse {
public success: boolean;
public msg: string;
}
然后将其作为通用参数传递给您的 http.post()
方法:
import { RegisterResponse } from './RegisterResponse'; // Or wherever it is..
export class AuthService {
constructor(private http:HttpClient) { }
registerUser(user){
let headers = new HttpHeaders();
headers.append('content-type','application/json');
return this.http.post<RegisterResponse>('http://localhost:8080/users/register', user, {headers:headers});
}
}
registerUser()
方法将为您 return 一个 Observable<RegisterResponse>
类型,因此当您订阅它时,您 data
变量将是 RegisterResponse
类型.但你也可以根据需要指定类型:
this._authservice.registerUser(user)
.subscribe((data: RegisterResponse) => {
if (data.success) {
// ...
}
else {
// ...
}
});
希望对您有所帮助。
您可以使用以下代码:
dataRegister:any={}
//Function register
this._authservice.registerUser(user)
.subscribe(data =>{
this.dataRegister = data;
if(this.dataRegister.success){
//
}
else{
//
}
}
user.js
router.post('/register', (req, res) => {
let newUser = new User({
name: req.body.name,
email: req.body.email,
username: req.body.username,
password: req.body.password
});
User.addUser(newUser, (err, user) => {
if (err) {
res.json({ "success": false, msg: 'Failed to register User' })
} else {
res.json({ "success": true, msg: "User Added" })
}
})
});
authservice.ts
import { catchError,map } from 'rxjs/operators';
import { Injectable } from '@angular/core';
import {HttpClient,HttpHeaders,HttpErrorResponse} from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class AuthService {
constructor(private http:HttpClient) { }
registerUser(user){
let headers = new HttpHeaders();
headers.append('content-type','application/json');
return this.http.post('http://localhost:8080/users/register',user,
{headers:headers})
}
}
register.component.ts
import { ValidateService } from './../../services/validate.service';
import { Component, OnInit } from '@angular/core';
import { FlashMessagesService } from 'angular2-flash-messages';
import {AuthService} from '../../services/auth/auth.service';
import {Router } from '@angular/router';
@Component({
selector: 'app-register',
templateUrl: './register.component.html',
styleUrls: ['./register.component.css']
})
export class RegisterComponent implements OnInit {
name:String;
username:String;
email:String;
password:String;
constructor(private _validate:ValidateService,
private _flashMessagesService: FlashMessagesService,
private _authservice:AuthService,
private _router:Router) { }
ngOnInit() {
}
onRegisterSubmit(){
const user={
name:this.name,
username:this.username,
email:this.email,
password:this.password,
}
this._authservice.registerUser(user)
.subscribe(data =>{
console.log(data);
**if(data.success){**
this._flashMessagesService.show('Registration Succesfull ! ',{cssClass:'alert-success',timeout:3000})
this._router.navigate(['/login']);
}else{
this._flashMessagesService.show('Oops Something went wrong! ',{cssClass:'alert-danger',timeout:3000})
this._router.navigate(['/register'])
}
})
}
}
错误
src/app/components/register/register.component.ts(49,17) 中的错误:错误 TS2339:属性 'success' 在类型 [=43 上不存在=].
数据提交成功,甚至 angular 也成功重定向到下一个组件,但这会出错。在register.component.ts while subscribing the 属性 success of the returned object in if statement if(data.success)
您可以对您的回复进行类型检查以避免此类错误。
创建一个 RegisterResponse
class 来包含您的 HTTP 响应的结构。
export class RegisterResponse {
public success: boolean;
public msg: string;
}
然后将其作为通用参数传递给您的 http.post()
方法:
import { RegisterResponse } from './RegisterResponse'; // Or wherever it is..
export class AuthService {
constructor(private http:HttpClient) { }
registerUser(user){
let headers = new HttpHeaders();
headers.append('content-type','application/json');
return this.http.post<RegisterResponse>('http://localhost:8080/users/register', user, {headers:headers});
}
}
registerUser()
方法将为您 return 一个 Observable<RegisterResponse>
类型,因此当您订阅它时,您 data
变量将是 RegisterResponse
类型.但你也可以根据需要指定类型:
this._authservice.registerUser(user)
.subscribe((data: RegisterResponse) => {
if (data.success) {
// ...
}
else {
// ...
}
});
希望对您有所帮助。
您可以使用以下代码:
dataRegister:any={}
//Function register
this._authservice.registerUser(user)
.subscribe(data =>{
this.dataRegister = data;
if(this.dataRegister.success){
//
}
else{
//
}
}