如何在Javascript中使用Azure移动服务自定义API进行Promises / Chaining

时间:2014-10-24 11:20:31

标签: javascript node.js azure

我试图弄清楚如何在AMS Javascript API中使用Promises。

这是我创建的两个功能,即“承诺”



function checkUsername(username, table) {
    
    return table.where({username: username}).read({
        success: function (results) {
            
            if (results.length === 0) {
                return true;   
            } else {
                return false;
            }
            
        },
        error: function(error) {
            return false;
        }
    });
    
}

function checkEmail(email, table) {
    
    return table.where({email: email}).read({
        success: function (results) {
            
            if (results.length === 0) {
                return true;   
            } else {
                return false;
            }
            
        },
        error: function(error) {
            return false;
        }
    });
    
}






checkUsername(body.username, accountsTable).then(function (results) {

  if (results) {
  
    return checkEmail(body.email, accountsTable);
  
  } else {
  
    response.send(400, {message: 'This username is already in use.'});
    
  }
  
}).then(function(results) {

  if (results) {
  
    response.send(200, {message: 'Can proceed with sign up.'});
    
  } else {
  
    response.send(400, {message: 'This email address is already in use.'});
    
  }
  
});




我试图像在Parse中那样使用承诺,但它显然不起作用。控制台日志不断吐出内部服务器错误,而.then()不是对象的函数。我假设我缺少一个要求或东西以便拥有Promises功能?

Error in script '/api/register.js'. TypeError: Cannot call method 'done' of undefined
at exports.post (D:\home\site\wwwroot\App_Data\config\scripts\api\register.js:30:59)
[external code]

1 个答案:

答案 0 :(得分:2)

我已经意识到我做错了什么。

我现在决定将Q节点模块用于我的承诺。



var q = require('q');

exports.post = function(request, response) {
    // Use "request.service" to access features of your mobile service, e.g.:
    //   var push = request.service.push;
    
    var tables = request.service.tables;
    
    var accountsTable = tables.getTable('Accounts');
    
    var params = request.body;
    
    checkUsername(params.username, accountsTable).then(function (result) {
       
        if (result.length === 0) {
         
            return checkEmail(params.email, accountsTable);
            
        } else {
         
            response.send(400, {message: 'This username is in use.'});
            
        }
        
    }).then(function (results) {
      
        if (results.length === 0) {
         
            return;
            
        } else {
         
            response.send(400, {message: 'This email address is already registered.'});
            
        }
        
    }).then(function () {
      
        response.send(200, {message: 'Username and email are unique. You can register!'});
        
    });
    
};

function checkUsername(username, table) {
    
    var deferred = q.defer();
    
    table.where({username: username}).read({
        success: function (result) {
            deferred.resolve(result);
        },
        error: function (error) {
            deferred.reject(error);   
        }
    });
    
    return deferred.promise;
    
}

function checkEmail(email, table) {
 
    var deferred = q.defer();
    
    table.where({email: email}).read({
        success: function (result) {
            deferred.resolve(result);
        },
        error: function (error) {
            deferred.reject(error);   
        }
    });
    
    return deferred.promise;
    
}