繁体   English   中英

如何使用node-apac链接多个寻呼请求?

[英]How to chain multiple paging request with node-apac?

我是Java和Nodejs的新手,我正在尝试设置一个服务器,该服务器可以通过node-apac节点包从amazon-product-api请求多个ItemPage。 到目前为止,我的http服务器已经启动并正在运行,并定义了一些路由。 我还可以请求一个ItemPage。 但是我很难链接请求。

Query的示例:

var query = {
    Title: theTitle,
    SearchIndex: 'Books',
    BrowseNodeId: '698198',
    Power: 'binding:not (Kindle or Kalender)',
    Sort: '-publication_date',
    ResponseGroup: 'ItemAttributes,Images',
    ItemPage: 1
};

编码:

AmazonWrapper.prototype.getAllPagesForQuery = function(theMethod, theQuery, theResultCallback) {    
    client.execute(theMethod, theQuery).then(function(theResult) {
        var pageCount = theResult.result.ItemSearchResponse.Items.TotalPages;
        var requests = [];
        for(var i = 2; i < pageCount; i++) {
            theQuery.ItemPage = i;
            requests.push(client.execute(theMethod, theQuery));     
        }
        Promise.all(requests).then(function(theResults) {       
            var data = theResults[0];
            for(var i = 1; i < theResults.length; i++) {
                var items = theResults[i].result.ItemSearchResponse.Items.Item;
                data.result.ItemSearchResponse.Items.Item.concat(items);
            }
            theResultCallback(data);
        });
    });
};

如您所见,我想从第一个请求中读取多少个Itempage可供我的ItemSearch使用,并为每个Itempage创建一个新请求。 不幸的是Promise.all(...)。then()从未被调用。

任何帮助表示赞赏

theQuery看起来像一个对象。 这样,当您执行theQuery.ItemPage = i并随后传递theQuery时,它将通过指针传递给您,您是将相同的对象传递给每个单个请求,而只是覆盖该对象上的ItemPage属性。 这不太可能正常工作。

我不知道确切是什么theQuery ,但是您可能需要复制它。

同样,您最好从.getAllPagesForQuery()返回一个.getAllPagesForQuery()而不要使用回调,因为您已经在使用.getAllPagesForQuery() 通过promise,错误处理和链接非常容易。

您没有完全披露足够的代码,但是以下是有关解决方法的一般想法:

AmazonWrapper.prototype.getAllPagesForQuery = function(theMethod, theQuery) {    
    return client.execute(theMethod, theQuery).then(function(theResult) {
        var pageCount = theResult.result.ItemSearchResponse.Items.TotalPages;
        var requests = [];
        for(var i = 2; i < pageCount; i++) {
            // make unique copy of theQuery object
            var newQuery = Object.assign({}, theQuery);
            newQuery.ItemPage = i;
            requests.push(client.execute(theMethod, newQuery));     
        }
        return Promise.all(requests).then(function(theResults) {       
            var data = theResults[0];
            for(var i = 1; i < theResults.length; i++) {
                var items = theResults[i].result.ItemSearchResponse.Items.Item;
                 data.result.ItemSearchResponse.Items.Item = data.result.ItemSearchResponse.Items.Item.concat(items);
            }
            return data;
        });
    });
};

// Usage example:
obj.getAllPagesForQuery(...).then(function(data) {
    // process returned data here
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM