NateW
NateW

Reputation: 2996

Node.js not reading HTML POST request

My node.js script is receiving HTML POST request calls from the google Chrome extension Postman but it's not seeing the key-value pairs. Keeps giving me

TypeError: Cannot read property 'username' of undefined
   at app.get.params.QueueUrl (/home/ec2-user/Outfitr/Server/index.js:45:53)
    at Layer.handle [as handle_request] (/home/ec2-user/Outfitr/Server/node_modules/express/lib/router/layer.js:82:5)
    at next (/home/ec2-user/Outfitr/Server/node_modules/express/lib/router/route.js:110:13)
    at Route.dispatch (/home/ec2-user/Outfitr/Server/node_modules/express/lib/router/route.js:91:3)
    at Layer.handle [as handle_request] (/home/ec2-user/Outfitr/Server/node_modules/express/lib/router/layer.js:82:5)
    at /home/ec2-user/Outfitr/Server/node_modules/express/lib/router/index.js:267:22
    at Function.proto.process_params (/home/ec2-user/Outfitr/Server/node_modules/express/lib/router/index.js:321:12)
    at next (/home/ec2-user/Outfitr/Server/node_modules/express/lib/router/index.js:261:10)
    at serveStatic (/home/ec2-user/Outfitr/Server/node_modules/express/node_modules/serve-static/index.js:59:14)
    at Layer.handle [as handle_request] (/home/ec2-user/Outfitr/Server/node_modules/express/lib/router/layer.js:82:5)

Here's my code. Let me know if you need anything else

app.post('/bscreateuser', function(request, response) {
  process.stdout.write("Attempted to create a --- BBBSSSSS ---- user  |  ");
  process.stdout.write("request is " + request.url);
  process.stdout.write("username is " + request.body.username);
  process.stdout.write("Password is " + request.body.password);
  bscreateUser(request.query.username, request.body.password);
  response.send('Success' );
});

function bscreateUser(username, password) {
  messageBody = 'create_user("' + username + '","' + password + '")';
  queueUrl = DAO_QUEUE_URL;
  // sys.puts("--- going for BS ---");
  sendSQSMessage(JSON.stringify(messageBody), queueUrl);
}

Upvotes: 0

Views: 767

Answers (1)

Michael Voznesensky
Michael Voznesensky

Reputation: 1618

 bscreateUser(request.query.username, request.body.password);

Should be:

 bscreateUser(request.body.username, request.body.password);

A good way to avoid this in the future is:

var body = request.body;
var username = body.username;
var password = body.password

And then just use the var username and var password in your code, much less error prone and a little more readable!

Upvotes: 3

Related Questions