POST使用mysql和nodejs

时间:2017-08-06 21:03:37

标签: mysql node.js express authentication post

我一直在使用coffeescript在快递服务器上进行注册路线。

以下是我迄今所做的事情:

app.post '/register', (req, res, next) ->
  email = req.body.email
  username = req.body.username
  password = req.body.password
  checkEmailUsernameExists(email, username).then((rows) ->
    if rows.length = 0
      console.log 'if'
      console.log rows.length
      createAccount email, username, password
    else
      console.log 'else'
      console.log rows.length
      res.send {'success': false, 'message': 'Email or username already registered!'}
    ).catch((err) ->
      throw err
      return
    )

createAccount = (mail, user, pass) ->
  query = "INSERT INTO users (email, username, password) VALUES (" + mail + "," + user + "," + pass + ")"
  bcrypt.genSalt 10, (err, salt) ->
    bcrypt.hash pass, salt, (err, hash) ->
      # Store hashed password in DB.
      connection.query query, (err, res) ->
        if err
          res.status(500).json { error: err }
        res.send {'success': true, 'message': 'Account registration success!'}

checkEmailUsernameExists = (mail, user) ->
  return new Promise (resolve, reject) ->
    query = "SELECT * FROM users WHERE email = ? OR username = ?"
    connection.query query, [mail, user], (err, row, fields) ->
      if err
        reject err
      resolve row

问题是:DB是空的,每次我发布到路由时,我都得到以下控制台输出:

  

否则0 POST / register 200 1.313 ms - 67

我不应该得到:

  

如果0 POST /寄存器200 1.313 ms - 67   ?   感谢您的耐心等待。

0 个答案:

没有答案