Angular POST在正文

时间:2017-11-04 07:32:57

标签: angular express

我知道这是一个重复的问题 Angular http post request always sends an empty object

但我已经尝试过这个答案,但对我来说并没有用。

这是我的代码:

服务:

registerUser(user: IUser) {
        console.log("Service 1");
        console.log(JSON.stringify(user));
        let headers = new Headers({'Content-Type': 'application/json'});
        let options = new RequestOptions({headers: headers});
        return this._http.post('/api/user', JSON.stringify(user), options)
                   .map((response: Response) => response.json())
                   .catch((error: Response) => Observable.throw(error));
    }

控制台输出:

Service 1
{"fname":"Abhisek","lname":"Mishra","email":"abhisek.mishra@gmail.com","pwd":"abhisek","dob":"1991-11-15","gender":"male"}

如果我在地图中添加一个控制台,它将不插入值并仅返回_id和_v

组件:

registerUser(user: IUser):void {
        console.log("Component 1");
        console.log(user);
        this._userConnection.registerUser(user)
            .subscribe((usr) => {
                console.log("component 2");
                console.log(usr);
                this.statusMessage = "Added !!";
                },
                  (error) => {
                      console.log(error);
                      this.statusMessage = error.statusText;
                  })
    }

表示'/ api / user'工作得很好。我试过邮差。但是当我从应用程序尝试时,控制台返回空对象

router.post('/user', function(req,res){
    console.log(req.body);
    var usr = new user();
    usr.fname = req.body.fname;
    usr.lname = req.body.lname;
    usr.email = req.body.email;
    usr.pwd = req.body.pwd;
    usr.dob = req.body.dob;
    usr.gender = req.body.gender;

    usr.save(function(err, usrl){
        if(err) {
            res.send("Error! " + err);
        } else {
            res.json(usrl);
        }
    })
});

0 个答案:

没有答案