node.js - how to fill a mysql database from a data entries of a form? -


i try fill data-base when person create account.my server in node.js. app.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 @ 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     //     app.get('/signup', route.signup);     // post     app.post('/signup', route.signuppost); 

route.js:

var signuppost = function(req, res, next) {    var user = req.body;    var usernamepromise = null;    usernamepromise = new model.user({username: user.username}).fetch();//verify existence of username on database      return usernamepromise.then(function(model) {       if(model) {          res.render('index1', {title: 'index', errormessage: 'username exists'});//verify if entered username exists in databas.       } else {          var password = user.password;     var email=user.email;      if(password.length<5){         res.render('index1',{title:'index',errormessage:'password weak'});//password must been > 5 caracters.     }     else if(verifmail(email) === false){         res.render('index1',{title:'index',errormessage:'email invalid'});//if email not valid,it show error     }      else{          var hash = bcrypt.hashsync(password);//crypt password           var signupuser = new model.user({username: user.username, password:hash,email:user.email,firstname:user.firstname,lastname:user.lastname});//add new row in database           signupuser.save().then(function(model) {             signinpost(req, res, next);          });     };         }    }); }; 

model.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; 

when person click button signup,a modal display enter firstname,lastname,email , password.

    <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> 

the page still reload long time , data did not been saved in database.

your signuppost function should return view or redirect route returns promise instead.

besides then() function should return promise or value returns nothing. , should add catch block;

i think misunderstood way promises work.

//... usernamepromise.then(function(model) {   if ( model ) throw new error('username exists');   else {     //...     if( ...) { throw new error('password weak'); }     else if( ...) { throw new error('email invalid'); }     else {       //...       return signupuser.save();     }   } }).then(function(model){   // want model , redirect   return res.redirect('/my/new/route');  }).catch(function(err){   // want err (eg: use flash or console.log(err)) , redirect   return res.redirect('/my/new/route');  }); //... 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -