Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在$http调用中使用then/catch_Javascript_Angularjs_Node.js_Http_Asynchronous - Fatal编程技术网

Javascript 在$http调用中使用then/catch

Javascript 在$http调用中使用then/catch,javascript,angularjs,node.js,http,asynchronous,Javascript,Angularjs,Node.js,Http,Asynchronous,我的节点后端具有以下端点: usersRoute.get('/get', function(req, res) { //If no date was passed in - just use todays date var date = req.query.date || dateFormat(new Date(), 'yyyy-mm-dd'), search = req.query.search; users.getAllUsers(dat

我的节点后端具有以下端点:

usersRoute.get('/get', function(req, res) {

    //If no date was passed in - just use todays date

    var date    = req.query.date || dateFormat(new Date(), 'yyyy-mm-dd'),
        search  = req.query.search;

    users.getAllUsers(date, search)
        .then(function(results) {
           res.json(results); 
        }, function(err) {
            res.status(500).json({
                success: false, 
                message: 'Server error.',
                data: []
            });
        });
});
我已将sql表名更改为其他名称,以触发
函数(err){}
部分

当我在我的服务中使用它时,它看起来是这样的:

function getUsers(date, search) {
            return $http.get('/api/users/get', {
                params: {
                    date: UtilsService.formatDate(date),
                    search: search
                }
            })
            .then(getData)
            .catch(handleErr);

            function getData(response) {
                return response.data;
            }

            function handleErr(err) {
                LoggerService.error('Could not retrieve users.', err ,'Ooops');
            }
        }
function getUsers(date, search) {
            isAdmin();

            vm.loading = true;
            vm.filteredUsers = [];

            return UsersService.getUsers(date, search).then(function(data) {
                vm.loading = false;

                allUsers = data || [];
                vm.filteredUsers = allUsers.slice(0, 50);
                vm.distribution = UsersService.getDistribution(allUsers);

                return vm.filteredUsers;
            });
        }
知道服务器将返回一个
http状态代码500
,我认为它将直接进入
捕获块。但它也会返回
块中
未定义的
数据

我在控制器中使用的服务如下:

function getUsers(date, search) {
            return $http.get('/api/users/get', {
                params: {
                    date: UtilsService.formatDate(date),
                    search: search
                }
            })
            .then(getData)
            .catch(handleErr);

            function getData(response) {
                return response.data;
            }

            function handleErr(err) {
                LoggerService.error('Could not retrieve users.', err ,'Ooops');
            }
        }
function getUsers(date, search) {
            isAdmin();

            vm.loading = true;
            vm.filteredUsers = [];

            return UsersService.getUsers(date, search).then(function(data) {
                vm.loading = false;

                allUsers = data || [];
                vm.filteredUsers = allUsers.slice(0, 50);
                vm.distribution = UsersService.getDistribution(allUsers);

                return vm.filteredUsers;
            });
        }
我的问题是,由于
然后
部分在我的
服务中被触发。我正在尝试对未定义的
进行切片


我的问题是:对于这种模式,som的最佳实践是什么

function getUsers(date, search, cb) {
      return $http.get('/api/users/get', {
           params: {
                date: UtilsService.formatDate(date),
                search: search
           }
      })
      .then(cb)
      .catch(handleErr);

      function handleErr(err) {
           LoggerService.error('Could not retrieve users.', err ,'Ooops');
      }
}
然后在你的控制器里

 UsersService.getUsers(date, search, function(data) {
        vm.loading = false;

        allUsers = data || [];
        vm.filteredUsers = allUsers.slice(0, 50);
        vm.distribution = UsersService.getDistribution(allUsers);
  });

问题是您从API中捕获错误,然后返回由
.catch
创建的承诺

快速示例

promise.then(function(data) {
  throw 'Some error';
}).catch(function (err) {
  console.log(err) // will output 'Some error'
}).then(function () {
  // This will run even though we have a catch before
});
那么,我们如何防止。那么我们很容易在
中抛出错误。catch

promise.then(function(data) {
  throw 'Some error';
}).catch(function (err) {
  console.log(err) // will output 'Some error'
  throw 'You shall not pass'
}).then(function () {
  // This will not run
});
因此,在您的情况下,您有两个选项,一个抛出我所说的错误,或者两个将
$q
服务注入到您的服务中:

function getUsers(date, search) {
    return $http.get('/api/users/get', {
        params: {
            date: UtilsService.formatDate(date),
            search: search
        }
    })
    .then(getData)
    .catch(handleErr);

    function getData(response) {
        return response.data;
    }

    function handleErr(err) {
        LoggerService.error('Could not retrieve users.', err ,'Ooops');
        return $q.reject(err);
    }
}

您确定您的服务器响应的是500吗?因为这肯定会触发捕获。@FabioAntunes我会稍微编辑一下我的问题。捕捉被触发了,但也触发了“then part”。现在我明白了,我为您提供了解决方案。现在看起来根本没有调用捕捉。谢谢!只有一个问题;我使用了第二个选项(使用$q)。它仍然在'then'中,并给我未定义的'@Nilzone'的'Cannot read property'data'。我已经创建了一个plunker,在这里你可以看到一切都很好,你是绝对正确的。我可能错过了什么。今天晚些时候我会仔细看看。谢谢你的例子!:)仍然有同样的问题。因为“then”中的响应对象未定义。我怀疑这与我的服务器发回数据的方式有关,因为常规http请求似乎可以正常工作