首页 > 解决方案 > 从角度 5 POST 到节点 js

问题描述

角 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 控制器

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",, email : sam@gmail.com, userName : sam, password : ...

在节点控制台中它给了我未定义但我检查了它们定义的所有值并对数据进行硬编码但仍然没有结果

请帮忙,谢谢。

这是控制台输出

{"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/".

标签: javascriptnode.jsangular

解决方案


推荐阅读