Angular 5
onSubmit() {
const goData = {
fullName : this.RegisterForm.get('fullname').value,
email : this.RegisterForm.get('email').value,
userName : this.RegisterForm.get('username').value,
password : this.RegisterForm.get('password').value
}
console.log(JSON.stringify(goData))
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/x-www-form-urlencoded',
})
};
this.http.post('http://localhost:7080/api/users/', goData , httpOptions)
.subscribe(data => {
console.log(data);
});
}
Express js controller
module.exports.usersAddOne = function(req, res){
console.log("Post new user");
User
.create({
fullName:req.body.fullName,
email:req.body.email,
userName:req.body.userName,
password:req.body.password
}, function(err, user){
if(err){
console.log("Error creating user"+err);
res
.status(404)
.json(err)
}else{
console.log("user created!", user);
res
.status(200)
.json(user);
}
});
}
我试图将一些数据以角度形式发布到节点js中,并且它会以无效的方式发布,但会发布任何无法解决的数据
所以,如果我发布goData,那么它会发布空白
{
"_id": "5ae6f2f3912ee5b81d6df23a",
"__v": 0
},
但我希望它像数据一样 fullName:“sam”,, 电子邮件:sam@gmail.com, userName:山姆, 密码:...
在节点控制台中它给我未定义但我检查了它们定义的所有值并且还硬编码了数据但仍然没有结果
请帮助,谢谢。
这是控制台输出
{"fullName":"sam","email":"sam","userName":"sam","password":"sam"}
register.component.ts:47 {__v: 0, _id: "5ae70e01ef0a592011724afa"}__v: 0_id: "5ae70e01ef0a592011724afa"__proto__: Object
XHR finished loading: POST "http://localhost:7080/api/users/".