问题描述
我正在使用SNowsql作为数据库,并提供了用户配置文件功能。当我输入用户信息时,用户名,密码,名字,姓氏都以未定义的形式返回。
我的app.js代码:
var sNowflake = require('sNowflake-sdk');
const express = require('express'),routes = require('./routes'),user = require('./routes/user'),http = require('http'),path = require('path');
//const methodoverride = require('method-override');
const session = require('express-session');
const app = express();
let bodyParser=require("body-parser");
var connection = sNowflake.createConnection( {
account: '***********',username: '***********',password: '***********',//creds commented out for security reasons.
database: '***********'
}
);
connection.connect(
function(err,conn) {
if (err) {
console.error('Unable to connect: ' + err.message);
}
else {
console.log('Successfully connected to SNowflake test account.');
// Optional: store the connection ID.
connection_ID = conn.getId();
}
}
);
//global.db = connection;
// all environments
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static('public'))
app.set('port',process.env.PORT || 8080);
app.set('views',__dirname + '/views');
app.set('view engine','ejs');
// app.use(express.static(path.join(__dirname,'public')));
app.use(session({
secret: 'keyboard cat',resave: false,saveUninitialized: true,cookie: { maxAge: 60000 }
}))
// development only
app.get('/',routes.index);//call for main index page
app.get('/signup',user.signup);//call for signup page
app.post('/signup',user.signup);//call for signup post
app.get('/reset',user.reset);//call for signup page
app.post('/reset',user.reset);//call for signup page
app.post('/forgot',user.forgot);//call for signup post
app.get('/forgot',user.forgot);//call for signup post
app.get('/login',routes.index);//call for login page
app.post('/login',user.login);//call for login post
app.get('/home/dashboard',user.dashboard);//call for dashboard page after login
app.get('/home/logout',user.logout);//call for logout
app.get('/home/profile',user.profile);//to render users profile
app.post('/home/profile',user.editprofile);//to render users profile
app.get('/verification/',user.verify);
//Middleware
app.listen(8080)
exports.editprofile = function (req,res) {
var userId = req.session.userId;
if (req.method == "POST") { //using res to initialize my accounts table variables.
// var post = req.session;
var userId = '2';
var post = req.body;
var name = post.user_name;
var pass = post.password;
var fname = post.first_name;
var lname = post.last_name;
var email = post.email;
console.log(name,pass,fname,lname); //Coming out as undefined
if (userId == null) {
res.redirect("/login");
return;
}
connection.execute({
sqlText: "UPDATE TEST.PUBLIC.ACCOUNTS SET FirsT_NAME =" + "'" +fname+ "'" + ",LAST_NAME = "
+ "'" + lname+ "'" + ",USER_NAME = " + "'" + name + "'" + ",PASSWORD = " + "'" + pass + "'" +
" WHERE EMAIL = 'fakemeail@gmail.com'",// UPDATE TEST.PUBLIC.ACCOUNTS SET FirsT_NAME = 'John',LAST_NAME = 'Smith',USER_NAME =
'Jsmith365',PASSWORD = '3Hotdogs' WHERE EMAIL = 'fakemeail@gmail.com';
complete: function (err,stmt,row) {
if (err) {
console.error('Failed to execute statement due to the following error: ' + err.message);
}
else {
console.log('Sucessfully updated profile');
res.redirect('/home/dashboard');
}
}
});
} else {
res.redirect('/home/dashboard');
}
};
我的数据库连接正常。之所以可行,是因为当我进入雪花数据库时,值更改为undefined,并且在前端显示为undefined。似乎正文解析器不知道我在说什么值,所以它是不确定的。最重要的是,用于实例化我的变量的示例代码可在前端和数据库上进行登录和注册。我觉得我想念什么。是在我的app.js中吗?任何帮助都会很棒!
解决方法
检查请求中是否包含content-type
标头
示例:
- Json数据包含
Content-Type: application/json
标头 - 对于x-www-form-urlencoded包含
Content-Type: application/x-www-form-urlencoded
标头