Gompro
Gompro

Reputation: 2315

express 4.x bodyparser isn't working

I installed latest version of body-parser(1.17.1) and set my code in proper order as others suggested.

  1. import express

  2. import body-parser

  3. app = express();

  4. app.use(bodyParser.json());

  5. app.use(bodyParser.urlencoded({extended : false}));

but I constantly get error "TypeError: Cannot read property 'password' of undefined".

Actually I can't imagine what the problem is.

I spent almost 10 hours keep tracking solution and I'm almost given up.

Is there any method to get my code work?

Help me some, please...

below is my code

    const express = require('express');
    const path = require('path');
    const session = require('express-session');
    const MySQLStore = require('express-mysql-session')(session);
    const bodyParser = require('body-parser');
    const passport = require('passport');
    const LocalStrategy = require('passport-local').Strategy;
    const pbkdf2Password = require('pbkdf2-password');
    const hasher = pbkdf2Password();
    const mysql = require('mysql');
    const connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '10rhrnak',
    database: 'users',
    });
    connection.connect();

    //crawler setting
    const http = require('http');
    const cheerio = require('cheerio');
    const iconv = require('iconv-lite');
    const fs = require('fs');

    const app = express();
    const port = process.env.PORT || '4200';
    app.set('port', port);
    app.listen(port, () => console.log(`API running on localhost:${port}`));

    app.use(express.static(path.join(__dirname, '../dist')));

    app.get('*', (req, res) => {
    res.sendFile(path.join(__dirname, '../dist/index.html'));
    });

    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: false }));
    app.use(session({
    secret: '@232t2g23',
    resave: false,
    saveUninitialized: true,
    store: new MySQLStore({
        host: 'localhost',
        port: 3306,
        user: 'root',
        password: '10rhrnak',
        database: 'users',
    })
    }));
    app.use(passport.initialize());
    app.use(passport.session());

    app.post('/auth/register', (res, req) => {

        hasher({password: req.body.password}, (err, pass, salt, hash) => {
            let user = {
                authId: 'local'+req.body.email,
                email: req.body.email,
                password: hash,
                salt: salt,
                displayName: req.body.displayName
            };
            let sql = 'INSERT INTO users SET ?';
            connection.query(sql, user, (err, results) => {
                if(err) {
                    console.log(err);
                    throw new Error("register error!");
                } else {
                    req.redirect('/');
                }
            });
        });   
    });

and also my front end code(angular2 html template)

<div class="loginbox">
  <div [hidden]="submitted">
    <header>MAILBOY</header>
    <form ngNoForm action="/auth/register" method="post">
      <input type="text" name="email" id="email" placeholder="이메일 주소"/>
      <input type="password" name="password" id="password" placeholder="비밀번호"/>
      <input type="text" name="displayName" id="displayName" placeholder="사용자 이름"/>
      <button type="submit" class="login">가입</button>
    </form>
  </div> 
</div>

Upvotes: 0

Views: 924

Answers (1)

robertklep
robertklep

Reputation: 203304

You switched the arguments for the route handler:

app.post('/auth/register', (res, req) => { ... });

That should be:

app.post('/auth/register', (req, res) => { ... });

Upvotes: 2

Related Questions