0

I have been trying to pass two parameter from my angular frontend to node api. However I am getting error on my node console that paramter value is missing or invalid when I run my app. Below here is node api code

app.post('/users', function(req, res) {
var username = req.body.username;
var orgName = req.body.orgName;
logger.debug('End point : /users');
logger.debug('User name : ' + username);
logger.debug('Org name  : ' + orgName);
if (!username) {
    res.json(getErrorMessage('\'username\''));
    return;
}
if (!orgName) {
    res.json(getErrorMessage('\'orgName\''));
    return;
}
var token = jwt.sign({
    exp: Math.floor(Date.now() / 1000) + parseInt(config.jwt_expiretime),
    username: username,
    orgName: orgName
}, app.get('secret'));
helper.getRegisteredUsers(username, orgName, true).then(function(response) {
    if (response && typeof response !== 'string') {
        response.token = token;
        res.json(response);
    } else {
        res.json({
            success: false,
            message: response
        });
    }
});

});

Here is my angular service code . For the sake of demonstration , I am passing dummy values from angular service

getEnrollmentId(userName,org) {
let headers = new Headers({ 'Content-Type': 'x-www-form-urlencoded' });
let options = new RequestOptions({ headers: headers });
let body = "'username=Barry&orgName=org2'";
return this.http.post('http://localhost:4000/users', body, options )
.map((res: Response) => res.json())
.catch((error:any) => Observable.throw(error.json().error || 'Server error shit'));

}

When I try to acheive same thing using curl query by calling node api and passing paramters , I am successfully able to post data and return response from api. Below here is curl query

 curl -s -X POST \
 http://localhost:4000/users \
 -H "content-type: application/x-www-form-urlencoded" \
 -d 'username=Jim&orgName=org1'

What am I doing wrong in my angular services?

Zoe
  • 27,060
  • 21
  • 118
  • 148
mohammad obaid
  • 415
  • 4
  • 16

1 Answers1

1

Pass the parameter as url search params , below code should help

let body = new URLSearchParams();
body.set('username', username);
body.set('orgName', orgName);
pritesh agrawal
  • 1,155
  • 8
  • 16
  • It didnt work . In my node api it is receiving undefined value for those parameters. Here is an screenshot of node console https://user-images.githubusercontent.com/26554206/29001906-f9664aac-7aae-11e7-9879-35f6128a2710.png – mohammad obaid Aug 06 '17 at 08:59
  • how you defined the variables(username and orgName) , which holds the value. – pritesh agrawal Aug 06 '17 at 09:18
  • here is gist link https://gist.github.com/mohammadobaid1/fd809e4cf8ece1078a6f2d57cbfb5b19 which contain my component code which calls my angular service and angular front-end html code which retrieve values from front end . – mohammad obaid Aug 06 '17 at 09:44
  • The html is for same component , why are you using @input ? . Its a simple two way binding with angular , define them as simple public variables , and it will be binded to front end with values and you will not get undefined variable – pritesh agrawal Aug 06 '17 at 10:00
  • I am doing something wrong on my frontend html code because when I print those variables values from my components , it is showing undefined . – mohammad obaid Aug 06 '17 at 10:00
  • dont use @input , simply define them as public username: string; public orgName: string; Angular will take care of the rest – pritesh agrawal Aug 06 '17 at 10:24