1

I'm using NodeJS/Express and EJS to create a form to an API route. When I try a POST request and call req.body.password & req.body.confirm , I get "undefined".

index.js

import http from 'http';
import express from 'express';
import bodyParser from 'body-parser';
import mongoose from 'mongoose';
import passport from 'passport';
const LocalStrategy = require('passport-local').Strategy;
var flash = require('connect-flash');

import config from './config';
import routes from './routes';

let app = express();
app.server = http.createServer(app);

//middleware
app.use(bodyParser.json({
  limit: config.bodyLimit
}));

//EJS VIEWS CODE
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(flash());

Part of my Controller w/ imports

import mongoose from 'mongoose';
import { Router } from 'express';
import Account from '../model/account';
import bodyParser from 'body-parser';
import passport from 'passport';
import config from '../config';
import express from 'express'
var async = require("async");
var nodemailer = require("nodemailer");
var crypto = require("crypto");

      if(req.body.password === req.body.confirm) {
          account.setPassword(req.body.password, function(err) {

EJS

<div class="row">
    <div class="col-md-12">
        <form action="http://localhost:3005/v1/account/reset/<%= token %>" method="POST">
          <legend>Reset Password</legend>
          <div class="form-group">
            <input type="password" name="password" value="" placeholder="New password" id="password" autofocus="autofocus" class="form-control"/>
          </div>
          <div class="form-group">
            <input type="password" name="confirm" value="" placeholder="Confirm password" id="confirm" class="form-control"/>
          </div>
          <div class="form-group">
            <button type="submit" class="btn btn-primary">Update Password</button>
          </div>
        </form>
    </div>
</div>

EDIT: Updated my controller w/ imports

cdh429
  • 297
  • 1
  • 11
  • 19

1 Answers1

5

Looking at your updated code snippet you seem to be using bodyParser.json which will only parse json payloads and not form data posted as body.

To accept form submissions you will also need to do something like:

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

It is working for you through postman because I am assuming that when using postman you are submitting json.

lorefnon
  • 12,112
  • 4
  • 54
  • 87
  • I have other parts API calls which works with req.body , just not this specific one. This is also the only one that uses EJS. I'm fairly new to EJS so I don't know if I'm missing anything. – cdh429 Feb 20 '18 at 21:03
  • Are you passing through your token variable when rendering your ejs file? – Enda Molloy Feb 20 '18 at 22:41
  • Yes , I'm posting it in the form action. I've identified the problem to the EJS form not posting it. I used POSTMAN and the code worked successfully. It's with the EJS it's not posting the req.body.password and the req.body.confirm @EndaMolloy – cdh429 Feb 21 '18 at 01:53
  • It worked! Just for future sake - do you mind explaining why that worked? @lorefnon – cdh429 Feb 21 '18 at 02:41
  • The middleware returned by bodyParser.json parses the body only if your body is in json format (and has compatible mimetype). The middleware returned by bodyParser.urlencoded parses url encoded form data which is what (non-multipart) forms in browser use by default. – lorefnon Feb 21 '18 at 02:44