我试图使用postgresql,Nodejs和Express制作注册表单。我现在只有一个问题,即插入数据。
我想将用户数据插入名为users(id int, username text, password text)
的表中。
我没有错误。数据库托管在Heroku上。插入的结果是:没有。没有错误。没有插入数据。
index.js MainJS
var pg = require('pg');
var bodyParser = require('body-parser');
var cool = require('cool-ascii-faces');
var express = require('express');
var app = express();
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
//BodyParser
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
// views is directory for all template files
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.get('/', function(request, response) {
app.locals.log = function(arg) {console.log("works");}
response.render('pages/index');
});
app.get('/db', function(request, response){
pg.connect(process.env.DATABASE_URL, function(err, client, done) {
client.query('SELECT * FROM test_table', function (err, result){
done();
if (err) {console.error(err); response.send("ERROR " + err);}
else {response.render('pages/db', {results :result.rows}); }
});
});
});
app.get('/register', function(request, response){
response.render('pages/register');
});
app.get('/cool', function(request, response){
response.send(cool());
});
//maps controller
var userController = require('./userController.js');
app.get('/users/register', userController.register);
app.post('/users/register', userController.register);
app.get('/users/login', userController.login);
app.post('/users/login', userController.login);
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
注册模板ejs register.ejs
<!DOCTYPE html>
<html>
<head>
<% include ../partials/header.ejs %>
</head>
<body>
<h1>Register</h1>
<% include ../partials/nav.ejs %>
<div class="container">
<h2>Register</h2>
<form method="POST" action="/users/register">
Username: <input type="text" name="username" id="username">
Password: <input type="password" name="password">
<input type="submit" value="Register">
</form>
</div>
</body>
</html>
userController.js
var pg = require('pg');
var registerUser = "INSERT INTO users (id, username, password) VALUES (1, 'Kris', 'blahbda');";
module.exports = {
login: function (req, res) {
pg.connect(process.env.DATABASE_URL, function(err, client, done) {
client.query('SELECT * FROM users', function (err, result){
done();
if (err) {console.error(err); res.send("ERROR " + err);}
else {res.render('pages/db', {results :result.rows}); }
});
});
},
register: function (req, res) {
var username = req.body.username;
pg.connect(process.env.DATABASE_URL, function(err, client, done) {
client.query(registerUser, function (err, result){
done();
if (err) {console.error(err); res.send("ERROR " + err);}
else {res.render('pages/db', {results :result.rows}); }
});
});
}
};
答案 0 :(得分:0)
我不做node.js但我的直接想法是创建了一个隐式事务,但在继续之前你没有提交或正确关闭连接。这在python和perl中很常见。
答案 1 :(得分:0)
尝试一下, 在userController.js内更改查询插入
var registerUser = 'INSERT INTO users ("id", "username", "password") VALUES ($1,$2,$3);', ['1', 'Kris', 'blahbda'];