Friday 19 May 2017

What is the correct syntax for my Angular 2 get request?

QUESTION:

I am trying to get a user by ID by creating an endpoint on the backend and querying it with the angular http service.

What is the correct syntax ?

Here is my current code.

CODE:

service

voteOn(poll: Poll, userID: string, choice: number) {
      this.http.get('/'+userID)
        .map(user => 
            user.votes.push({poll, choice });
            const body = JSON.stringify(user);
            const headers = new Headers({'Content-Type': 'application/json'});
            const token = localStorage.getItem('token')
                ? '?token=' + localStorage.getItem('token')
                : '';
            return this.http.patch('https://voting-app-10.herokuapp.com/user'+token, body, {headers: headers})
                .map((response: Response) => response.json())
                .catch((error: Response) => {
                    this.errorService.handleError(error);
                    return Observable.throw(error);
                })
                .subscribe();
          )
    }

route

router.get('/:userid', function (req, res, next) {
  var userId = req.params.userid;
  UserModel.findById(userID, function (err, user) {
    return res.send(user);
  });
});



via Coder1000

No comments:

Post a Comment