我正在研究MEAN应用程序,其简单的注册和登录应用程序 我试图通过表单保存帖子,并且在控制台中提交表单后出现此错误:Angualr的HttpErrorResponse以及后端控制台,我正在获取if(err)throw err; ^ 错误:参数非法:未定义,字符串
我遵循angular 2教程,但我正在使用angualr 6
这是教程链接https://www.youtube.com/watch?v=dFftMN32jyQ&list=PLillGF-RfqbZMNtaOXJQiDebNXjVapWPZ&index=7
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/map';
@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:3000/users/register','user', {headers: headers})
.map(res => res);
}
}
import { Component, OnInit } from '@angular/core';
import { ValidateService } from '../../services/validate.service';
import { AuthService } from '../../services/auth.service';
import { FlashMessagesService } from 'angular2-flash-messages/module/flash-messages.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 validateService: ValidateService, private flashMessage: FlashMessagesService,
private authService: AuthService, private router: Router) { }
ngOnInit() {
}
onRegisterSubmit(){
const user = {
name: this.name,
email: this.email,
username: this.username,
password: this.password
}
//Required Field
if(!this.validateService.validateRegister(user)){
//console.log('Please Fill in all feilds');
this.flashMessage.show('Please Fill in all feilds',{cssClass:'alert-danger', timeout: 3000});
return false;
}
//Required Email
if(!this.validateService.validateEmail(user.email)){
// console.log('Please use a valid email');
this.flashMessage.show('Please use a valid email',{cssClass:'alert-danger', timeout: 3000});
return false;
}
//Register User
this.authService.registerUser(user).subscribe(data =>{
if(data){
this.flashMessage.show('You are now registered and can log in',{cssClass:'alert-success', timeout: 3000});
this.router.navigate(['/login']) ;
}else{
this.flashMessage.show('Something went wrong',{cssClass:'alert-danger', timeout: 3000});
this.router.navigate(['/register']) ;
}
});
}
}
<h2 class="page-header">Register</h2>
<form (submit)="onRegisterSubmit()">
<div class="form-group">
<label>Name</label>
<input type="text" [(ngModel)]="name" name="name" class="form-control">
</div>
<div class="form-group">
<label>Username</label>
<input type="text" [(ngModel)]="username" name="username" class="form-control">
</div>
<div class="form-group">
<label>Email</label>
<input type="email" [(ngModel)]="email" name="email" class="form-control">
</div>
<div class="form-group">
<label>Password</label>
<input type="password" [(ngModel)]="password" name="password" class="form-control">
</div>
<input type="submit" class="btn btn-primary" value="Submit">
</form>
我们非常感谢您的帮助。
答案 0 :(得分:0)
您似乎是在传入变量中发布字符串“ user”,而不是对象用户。
此:
return this.http.post('http://localhost:3000/users/register','user', {headers: headers})
应该是这样:
return this.http.post('http://localhost:3000/users/register', user, {headers: headers})