【问题标题】:How to use Promises/Chaining with Azure Mobile Services Custom API in Javascript如何在 JavaScript 中通过 Azure 移动服务自定义 API 使用 Promises/Chaining
【发布时间】:2014-10-24 21:18:35
【问题描述】:

我正在尝试弄清楚如何将 Promises 与 AMS Javascript API 一起使用。

这是我创建的两个将被“承诺”的函数

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 中一样使用 Promise,但它显然不起作用。控制台日志不断吐出内部服务器错误,并且 .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]

【问题讨论】:

    标签: javascript node.js azure


    【解决方案1】:

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

    我现在决定使用 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;
        
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-12
      • 1970-01-01
      相关资源
      最近更新 更多