Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript express 4.x bodyparser isn';行不通_Javascript_Node.js_Express_Body Parser - Fatal编程技术网

Javascript express 4.x bodyparser isn';行不通

Javascript express 4.x bodyparser isn';行不通,javascript,node.js,express,body-parser,Javascript,Node.js,Express,Body Parser,我安装了最新版本的body parser(1.17.1),并按照其他人建议的顺序设置代码 进口快车 导入体解析器 app=express() use(bodyParser.json()) use(bodyParser.urlencoded({extended:false})) 但我经常遇到错误“TypeError:无法读取未定义的属性‘password’” 事实上,我无法想象问题出在哪里 我花了近10个小时跟踪解决方案,几乎放弃了 有什么方法可以让我的代码工作吗 请帮我一些 下面是我的代码

我安装了最新版本的body parser(1.17.1),并按照其他人建议的顺序设置代码

  • 进口快车

  • 导入体解析器

  • app=express()

  • use(bodyParser.json())

  • use(bodyParser.urlencoded({extended:false}))

  • 但我经常遇到错误“TypeError:无法读取未定义的属性‘password’”

    事实上,我无法想象问题出在哪里

    我花了近10个小时跟踪解决方案,几乎放弃了

    有什么方法可以让我的代码工作吗

    请帮我一些

    下面是我的代码

        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('/');
                    }
                });
            });   
        });
    
    还有我的前端代码(angular2 html模板)

    
    邮差
    가입
    
    您切换了路由处理程序的参数:

    app.post('/auth/register', (res, req) => { ... });
    
    这应该是:

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

    您已切换路由处理程序的参数:

    app.post('/auth/register', (res, req) => { ... });
    
    这应该是:

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

    你是如何测试这条路线的?你是如何发送数据的?它是在请求正文还是查询字符串中?我有一个angular2模板用于前端这是angular2 html模板文件MAILBOY가입 我通过输入表格发送数据。你是如何测试这条路线的?你是如何发送数据的?它是在请求正文还是查询字符串中?我有一个angular2模板用于前端这是angular2 html模板文件MAILBOY가입 我通过输入表格发送数据。