gpt4 book ai didi

mysql - 如何从表单的数据条目填充mysql数据库?

转载 作者:行者123 更新时间:2023-11-29 20:58:32 27 4
gpt4 key购买 nike

当有人创建帐户时,我尝试填充数据库。我的服务器位于 node.js 中。应用程序.js:

var http=require("http");
var express = require('express');
var app = express();
var server = http.createServer(app);
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var bcrypt = require('bcrypt-nodejs');
var ejs = require('ejs');
var path = require('path');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;



// routes
var route = require('./route');
// model
var Model = require('./model');




app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(cookieParser());
app.use(bodyParser.urlencoded({extended:false}));
app.use(session({secret: 'secret strategic xxzzz code',
resave:false,
saveUninitialized:false
}));
app.use(passport.initialize());
app.use(passport.session());




server.listen(3000,function(){
console.log("server listen at localhost:3000");});
passport.use(new LocalStrategy(function(username, password, done) {
new Model.User({username: username}).fetch().then(function(data) {
var user = data;
if(user === null) {
return done(null, false, {message: 'Invalid username or password'});
} else {
user = data.toJSON();
if(!bcrypt.compareSync(password, user.password)) {
return done(null, false, {message: 'Invalid username or password'});
} else {
return done(null, user);



}
}
});
}));



passport.serializeUser(function(user, done) {
done(null, user.username);
});

passport.deserializeUser(function(username, done) {
new Model.User({username: username}).fetch().then(function(user) {
done(null, user);
});
});

// signup
// GET
app.get('/signup', route.signUp);
// POST
app.post('/signup', route.signUpPost);

路由.js:

var signUpPost = function(req, res, next) {
var user = req.body;
var usernamePromise = null;
usernamePromise = new Model.User({username: user.username}).fetch();//verify the existence of username on the database


return usernamePromise.then(function(model) {
if(model) {
res.render('index1', {title: 'Index', errorMessage: 'username already exists'});//verify if the entered username exists in the databas.
} else {
var password = user.password;
var email=user.email;

if(password.length<5){
res.render('index1',{title:'index',errorMessage:'password so weak'});//password must been > 5 caracters.
}
else if(verifMail(email) === false){
res.render('index1',{title:'index',errorMessage:'email invalid'});//if the email is not valid,it show an error
}

else{
var hash = bcrypt.hashSync(password);//crypt the password

var signUpUser = new Model.User({username: user.username, password:hash,email:user.email,firstname:user.firstname,lastname:user.lastname});//add a new row in the database

signUpUser.save().then(function(model) {
signInPost(req, res, next);
});
};
}
});
};

模型.js:

    var DB = require('./db').DB;

var User = DB.Model.extend({
tableName: 'newuser',
idAttribute: 'userId',
});

module.exports = {
User: User
};

db.js:

 var Bookshelf = require('bookshelf');

var config = {
host: 'localhost',
user: 'root',
password: '',
database: 'Users',
charset: 'UTF8_GENERAL_CI'
};

var DB = Bookshelf.initialize({
client: 'mysql',
connection: config
});

module.exports.DB = DB;

当一个人单击按钮进行注册时,将显示一个模式以输入他的名字、姓氏、电子邮件和密码。

    <div id="myModal" class="modal fade"   role="dialog" >

<div class="model-content">
<div class="modal-header" >
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 id="myModalLabel">Modal header</h3>
</div>
<div class="modal-body">
<!-- form -->
<form id="register-form" role="form" method="post" action="/signup">


<div class="form-group">

<input type="text" class="form-control" id="username" placeholder="User name">
</div>

<div class="form-group">

<input type="text" class="form-control" id="firstname" placeholder="First name">
</div>
<div class="form-group">
<input type="text" class="form-control" id="lastname" placeholder="Last name">
</div>
<div class="form-group">

<input type="email" class="form-control" id="email" placeholder="Email">
</div>
<div class="form-group">

<input type="password" class="form-control" id="password" placeholder="Password">
</div>

<div class="emaillogin">
<button type="submit" class="btn btn-default email-signin-button">
Submit
</button>
</div>

</form>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-default email-signin-button" data-dismiss="modal">
Close
</button>

</div>
</div>
</div>

页面仍然重新加载很长时间并且数据没有保存到数据库中。

最佳答案

您的signUpPost函数应该返回一个 View 或重定向到另一个路由,但它返回一个 promise 。

此外,then() 函数应该始终返回一个 promise 或一个值,但它什么也不返回。并且您应该始终添加一个 catch block ;

我认为您误解了 Promise 的工作方式。

//...
usernamePromise.then(function(model) {
if ( model ) throw new Error('username already exists');
else {
//...
if( ...) { throw new Error('password so weak'); }
else if( ...) { throw new Error('email invalid'); }
else {
//...
return signUpUser.save();
}
}
}).then(function(model){
// do what you want with model and redirect
return res.redirect('/my/new/route');
}).catch(function(err){
// do what you want with err (eg: use flash or console.log(err)) and redirect
return res.redirect('/my/new/route');
});
//...

关于mysql - 如何从表单的数据条目填充mysql数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37425935/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com