Node.js没有使用promise第一次更改数据
我有这个问题白我的代码,我有一种数据,我需要获得最新的时候,用户要求它,我使用承诺获得正确的数据,这部分工作正常。
我的下一个问题是我第一次要求的数据,它的返回没有什么,但是,如果我点击agin的数据,它返回正确的数据,所以这里有一些错误。
我的承诺function
var insert = new Promise(function(fulfill) { fulfill('test'); });
我的出口模块
exports.signup = function(db, user_conf) { var self = this; defineUser(user_conf); insert.then(function(result) { self.json_response = result; console.log(result); }).catch(function(e) { console.log(e); }); return self.json_response; }
我的快速路线function
router.post('/signup', function(req, res, next) { var post = req.body; json_response = users.signup(req.db, { 'fullname' : post["account-fullname"], 'username' : post["account-username"], 'email' : post["account-email"], 'password' : post["account-password"], 'retype-password' : post["account-retype-password"], 'accept-terms' : post["accept-terms"] }); res.send(json_response); });
我需要它我需要我的注册部分的响应,以了解用户可以创build或有一种validation错误用户需要知道用户可以创build之前。
closures我的头顶 – 尝试这样的事情
exports.signup = function(db, user_conf) { defineUser(user_conf); // I'm assuming this is synchronous return insert.then(function(result) { // do something here maybe? if not then you only need to return insert; return result; // return a result }); } // if nothing is being done in the then callback above, this can be simplified to exports.signup = function(db, user_conf) { defineUser(user_conf); // I'm assuming this is synchronous return insert; } router.post('/signup', function(req, res, next) { var post = req.body; users.signup(req.db, { 'fullname' : post["account-fullname"], 'username' : post["account-username"], 'email' : post["account-email"], 'password' : post["account-password"], 'retype-password' : post["account-retype-password"], 'accept-terms' : post["accept-terms"] }).then(function(json_response) { res.send(json_response); }); });