2017-02-19 88 views
-2

我很難理解工作如何承諾,我試着用express創建承諾,在mysql上查詢並獲得結果。我讀到,讓這個同步並不酷。我正在嘗試做一個小遊戲,這條路線是爲了註冊。Nodejs + express promisse

var lang = require('../lang.js'); 
var config = require('../config.js'); 
var fieldValidator = require('../libs/field_validator'); 
var microtime = require('microtime'); 
var crypto = require('crypto'); 
//Presets 
var lang = new lang(); 
var rlang = lang.returnMessage; 
var fieldValidator = new fieldValidator(); 
var moment = require('moment'); 

module.exports = function(app, my){ 
    //Primeiro os registros 
    /// Account Register 
    //// 
    var check_username = function(username){ 
     return new Promise(function(resolve, reject) { 
      my.query('SELECT username FROM 4h_users WHERE ?', username, function (error, result, fields) { 
       if (error) { 
        reject({"status": "error", "code":10 ,"message": rlang(lang, 10)}); 
       }else{ 
        if(result.lenght > 0){ 
         reject({"status": "error", "code":10 ,"message": rlang(lang, 10)}); 
        }else{ 
         resolve(true); 
        } 
       }; 
      }); 
     }); 
    } 
    check_username().then(function(id_user) { 
     console.log(value); 
    }, function(reason) { 
     res. 
    }); 


    app.post('/api/usuario/registrar', function(req, res){ 
     if(!req.body.lang){ 
      return res.send({"status": "error", "code":1 ,"message": 'Blank Language'}); 
     }else{ 
      var lang = req.body.lang; 
     } 
     if(!req.body.recode){ 
      return res.send({"status": "error", "code":4 ,"message": rlang(lang, 4)}); 
     }else if(!req.body.username){ 
      return res.send({"status": "error", "code":1 ,"message": rlang(lang, 1)}); 
     }else if(!req.body.password){ 
      return res.send({"status": "error", "code":3 ,"message": rlang(lang, 3)}); 
     }else if(!req.body.email){ 
      return res.send({"status": "error", "code":5 ,"message": rlang(lang, 5)}); 
     }else if(req.body.reemail != req.body.email){ 
      return res.send({"status": "error", "code":6 ,"message": rlang(lang, 6)}); 
     } 

     my.query('SELECT email FROM 4h_users WHERE ?', req.body.email, function (error, result, fields) { 
      if (error) { 
       return res.send({"status": "error", "code":10 ,"message": rlang(lang, 10)}); 
      }else{ 
       return res.send({"status": "error", "code":13 ,"message": rlang(lang, 13)}); 
      }; 
     }); 

     if(!fieldValidator.username.isValid(req.body.username)){ 
      return res.send({"status": "error", "code":9 ,"message": rlang(lang, 9)}); 
     }else if(!fieldValidator.email.isValid(req.body.email)){ 
      return res.send({"status": "error", "code":7 ,"message": rlang(lang, 7)}); 
     }else if(!fieldValidator.password.isValid(req.body.password)){ 
      return res.send({"status": "error", "code":8 ,"message": rlang(lang, 8)}); 
     } 

     var ip = generate_ip(); 
     var salt = crypto.createHash('sha256').update(microtime.now()+req.body.username).digest("hex"); 
     var password = crypto.createHash('sha512').update(salt+req.body.password).digest("hex"); 

     var user = { 
      username: req.body.username, 
      email: req.body.email, 
      password: password, 
      salt: salt, 
      token_expiration: microtime.now(), 
      money: config.default_bonus.money, 
      hcoins: config.default_bonus.hcoins, 
      register_date: moment().format('YYYY-MM-DD HH:mm:ss'), 
      token: crypto.createHash('sha256').update(microtime.now()+req.body.username).digest("hex"), 
      token_expiration: microtime.now()+config.expiration.login, 
      offline_mode_expiration: 0 
     }; 

     //Check if machine ip exists and generate another one 
     check_ip_status(my, ip); 

     // Time to mysql 

     //Add new user to mysql 
     my.query('INSERT INTO 4h_users SET ?', user, function (error, result, fields) { 
      if (error) { 
       console.log(error); 
       return res.send({"status": "error", "code":10 ,"message": rlang(lang, 10)}); 
      }else{ 
       var machine = { 
        ip: ip, 
        id_user: result.insertId 
       }; 
       //Add new machine to mysql 
       my.query('INSERT INTO 4h_machines SET ?', machine, function (error, result, fields) { 
        if (error) { 
         console.log(error); 
         return res.send({"status": "error", "code":10 ,"message": rlang(lang, 10)}); 
        }else{ 
         return res.send({"status": "success", "code":11 ,"message": rlang(lang, 11)}); 
        }; 
       }); 
      }; 
     }); 
    }); 
} 


function generate_ip(){ 
    return (Math.floor(Math.random() * 254) + 1) + "." + (Math.floor(Math.random() * 254) + 1) + "." + (Math.floor(Math.random() * 254) + 1) + "." + (Math.floor(Math.random() * 254) + 1); 
} 
function check_ip_status(my, ip){ 
    my.query('SELECT ip FROM 4h_machines WHERE ?', ip, function (error, result, fields) { 
     if (error) { 
      return res.send({"status": "error", "code":10 ,"message": rlang(lang, 10)}); 
     }else{ 
      if(result.lenght > 0){ 
       ip = generate_ip(); 
       check_ip_status(ip); 
      } 
     }; 

    }); 
} 
//id_user, username, salt, password, email, avatar_url, 4h_coins, money, last_login, register_date, token, token_expiration, reputation, actions, email_verify, modo_offline_expiration 

,如果您有其他提示此代碼,請告訴我,我在學習和任何幫助是我 感謝

+0

你應該總是等待MySQL查詢結果。因此將代碼分成幾塊。請閱讀此頁面; https://blog.risingstack.com/asynchronous-javascript/ –

+1

您似乎試圖向此請求發送多個回覆。當你執行'my.query()',然後在響應中有一個if/else時,你可以用'res.send()'發送一個響應,以獲得所有可能的結果。然後,你在函數的後面也有'res.send()'。你不能那樣做。您只發送一個響應每個請求。所以,這裏的邏輯流程是錯誤的。另外,你需要在'my.query()'回調中繼續處理,而不是在它之後。 – jfriend00

回答

2

非常有用的,如果你有這個代碼,請告訴另一個祕訣我,我正在學習和任何幫助對我非常有用,謝謝

這是一個非常普遍的問題,可以概括爲:我如何瞭解承諾。有約承諾在Stack Overflow上的問題和答案,我可以推薦很多