简体   繁体   English

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

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

I'm new to Javascript and Nodejs and I am trying to setup a server which can request multiple ItemPages from amazon-product-api via the node-apac node package. 我是Java和Nodejs的新手,我正在尝试设置一个服务器,该服务器可以通过node-apac节点包从amazon-product-api请求多个ItemPage。 So far my http server is up and running with some routes defined. 到目前为止,我的http服务器已经启动并正在运行,并定义了一些路由。 I can also request a single ItemPage. 我还可以请求一个ItemPage。 But I have trouble chaining the requests. 但是我很难链接请求。

example for theQuery: Query的示例:

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

the code: 编码:

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);
        });
    });
};

As you see I want to read from the first request how many Itempages are available for my ItemSearch und create a new request for each Itempage. 如您所见,我想从第一个请求中读取多少个Itempage可供我的ItemSearch使用,并为每个Itempage创建一个新请求。 Unfortunately Promise.all(...).then() is never called. 不幸的是Promise.all(...)。then()从未被调用。

Any help is appreciated 任何帮助表示赞赏

theQuery looks like an object. theQuery看起来像一个对象。 As such, it is passed by pointer to when you do theQuery.ItemPage = i and then you pass theQuery , you're passing the same object to every single request and just overwriting the ItemPage property on that one object. 这样,当您执行theQuery.ItemPage = i并随后传递theQuery时,它将通过指针传递给您,您是将相同的对象传递给每个单个请求,而只是覆盖该对象上的ItemPage属性。 This is unlikely to work properly. 这不太可能正常工作。

I don't know exactly what theQuery is, but you probably need to make copy of it. 我不知道确切是什么theQuery ,但是您可能需要复制它。

Also, you may as well return a promise from .getAllPagesForQuery() rather than use a callback since you're already using promises. 同样,您最好从.getAllPagesForQuery()返回一个.getAllPagesForQuery()而不要使用回调,因为您已经在使用.getAllPagesForQuery() Error handling and chaining is a whole lot easier with promises all the way through. 通过promise,错误处理和链接非常容易。

You don't quite disclose enough code, but here's the general idea for how to fix: 您没有完全披露足够的代码,但是以下是有关解决方法的一般想法:

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