AngularJS - 正确格式化异步调用

时间:2017-07-17 21:13:09

标签: javascript angularjs node.js asynchronous

新手在这里。

我试图理解我需要在控制器中构建异步调用以适应我的特定用例:

考虑我项目中“service.js”中Angular模块的以下代码片段:

function getSearchObjects(projectName, title) {
    var payload = JSON.stringify({
        "title": title
    });

    var request = $http({
        method: 'post',
        url: URL + '/search/' + projectName,
        data: payload
    });
    return request.then(handleSuccess, handleError);
};

function runQuery(projectName, fromDate, toDate, sort, direction, columns) {
    var from = Date.parse(fromDate);
    var to = Date.parse(toDate);

    var payload = JSON.stringify({
                                    "fromDate": from,
                                    "toDate": to,
                                    "sort": sort,
                                    "direction": direction,
                                    "columns": columns
                                });
    console.log(payload);
    var request = $http({
        method: 'post',
        url: URL + '/query/' + projectName,
        data: payload
    });
    return request.then(handleSuccess, handleError);
}

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

function handleError(response) {
    if (!angular.isObject( response.data ) || !response.data.error) {
        return( $q.reject( "An unknown error occurred." ) );
    }
    return $q.reject( response.data.error );
};

});

在我的控制器中,我正在尝试解决以下问题:

$scope.submit = function() {
  var objectProperties = exportsStorageService.getSearchObjects($scope.selected.project.name, $scope.selected.search)
    .then(function(result) {
        exportsStorageService.runQuery($scope.selected.project.name, $scope.selected.start_date, $scope.selected.end_date, objectProperties.sort, objectProperties.direction, objectProperties.columns)
    },            
    function(error) {
        console.log(error);
    });
};

getSearchObjects匹配在我的UI中选择的标题($ scope.selected.search),并从API调用中获取以下更详细的对象:

{ title: 'Duplication Example',
sort: '#_traac-timestamp',
direction: 'desc',
columns: [ '#_traac-remote_ip', 'c-platform-m-distinct-id_s', '_type' ] }

我试图获取从getSearchObjects返回的属性,并将它们与我的UI中的一些用户选择的值一起传递给runQuery,然后runQuery将数据从数据库返回给用户,但是当我检查传递给runQuery的值时在我的控制器中的上述逻辑,我得到以下值。我试图传递给runQuery的所有objectProperties值都是未定义的:

project_name: "Example Project"
start_date: 1499770800000
end_date: 1499943600000
sort: undefined
direction: undefined
columns: undefined

我一直在尝试调试这个,但我太新了,不能使用Angular和异步调用来真正理解我做错了什么。我最好的猜测是,在从getSearchObjects检索的值附加到objectProperties之前,我调用runQuery。或者我错误地引用了objectProperties变量中的属性。

有人可以帮我解决这个问题,更好地了解我做错了吗?

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

执行此操作时:

var objectProperties = some async function...

您将async函数的承诺分配给变量,不是其结果

结果来自.then,就像你声明的那样:

.then(function(result) { ... }

因此,请尝试使用objectProperties.sort, objectProperties.direction, objectProperties.columns:)

,而不是result.sort, result.direction, result.columns

如果您是Promises的新手,请查看此simple, but great tutorial

修改

根据您的评论,您在response.data内接收以下对象:

{"objectMatch": {
    "title": "doc-event",
    "sort": "#_traac-timestam‌​p",
    "direction": "desc‌​",
    "columns": [
                 "m-doc-‌​name_s",
                 "m-user_s",
                 "‌​m-full-action-type_s‌​",
                 "m-event-action-de‌​scriptor_s"
               ]}
}

所以你有:响应>数据> objectMatch>你想要的属性

您在handleSuccess函数中提取的response.data:

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

所以在这里,您的resultresponse.data,其中包含属性objectMatch

$scope.submit = function() {
  var objectProperties = exportsStorageService.getSearchObjects($scope.selected.project.name, $scope.selected.search)
    .then(function(result) {
        ...
    },
    ...

如果所有这些都是正确的,您应该能够使用result.objectMatch.<sort, direction or columns>访问所需的值,例如:

exportsStorageService.runQuery($scope.selected.project.name, $scope.selected.start_date, $scope.selected.end_date,
    result.objectMatch.sort, result.objectMatch.direction, result.objectMatch.columns)