Tuesday, 14 March 2017

NodeJS - Updating object on request

I have the following object:

 var user = {

        firstName: req.body.first_name,
        lastName:  req.body.last_name,
        email:     req.body.email,
        password: "",
        id:         "", 
    };

Now what I'm trying to do is post a request to the API and if the user is successfully saved in the database, it will return a user id as well as a password (Which can then be emailed to the person)..

request.post({

       url: process.env.API_SIGNEDIN_ENDPOINT + "users/store",
       form: user,
       headers: {
         Authorization: "Bearer " + req.session.authorization
       }

    }, function(error, response, body) {

       // Check the response
       var theResponse = JSON.parse(response.body);

       if(theResponse.code == 400)
       {
          var error = [
            {param: "email", msg: "This email address has already been taken!", value: ""}
          ];

          res.render("create", {
              errors: error,
          });
       }else{

         var theUser = JSON.parse(body);
         user.passwird = theUser.password;
         user.id       = theUser.id;
       }
    });

This is working fine, however, whenever I try to output user it's not updating the user object outside of this post. The user object is fine and it's working, the issue seems to be from when I try and access the user from this result callback. Any ideas?



via Phorce

No comments:

Post a Comment