我安装了最新版本的body-parser(1.17.1)并按正常顺序设置我的代码。
import express
导入正文解析器
app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));
但我经常收到错误" TypeError:无法读取属性'密码'未定义"。
实际上我无法想象问题是什么。
我花了差不多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模板)
<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>
答案 0 :(得分:2)
您切换了路由处理程序的参数:
var id = "123",
name = "This Is A Name";
var XHR = new XMLHttpRequest(),
method = "GET",
url = "crudops.aspx?StudentId="+id+"&StudentName="+name+"&operation=insert";
XHR.open(method, url, true); // << or omit async (anyway defaults to true)
XHR.onreadystatechange = function () {
if(XHR.readyState === XMLHttpRequest.DONE && XHR.status === 200) {
document.getElementById("TableDiv").innerHTML = XHR.responseText;
// Use meaningful var names
// var tableWrapper = document.getElementById("TableDiv").innerHTML;
// BTW since at this point your table is (hopefully) in the DOM
// you can go get it directly
var table = document.getElementById("studenttable");
if(!table) return; // Do nothing if no such Element in DOM
var row = table.insertRow(0);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
cell1.innerHTML = id;
cell2.innerHTML = name;
}
};
XHR.send();
应该是:
app.post('/auth/register', (res, req) => { ... });