Node.js使用q框架递归调用多个GET请求

时间:2016-10-31 17:18:37

标签: javascript node.js recursion q deferred

在搜索stackoverflow和Q文档后,遗憾的是我仍然不理解q.ninvoke函数。我想知道是否有人可以解释我的情况如何:

我正在获取fetchXml请求的结果,最多返回5000个条目。我想实现一个调用自身(通过链)的方法,直到服务器不再响应pagingcookie。

最后,所有结果都应该在递归到递归顶部的过程中得到连贯...

var tryReexecuteForNext5000Entries = function(entitySetName, fetchXml, args){
    var deferred = q.defer();
    var response = args[0];    
    var jsonBody = args[1];
    var fetchXmlPagingCookieResponse = jsonBody["@Microsoft.Dynamics.CRM.fetchxmlpagingcookie"];

    if(!fetchXmlPagingCookieResponse){        
        deferred.resolve(args);
        return deferred.promise;
    }

    var pagingArgs = getPagingArgs(fetchXmlPagingCookieResponse);
    var pageNumber = pagingArgs[0];
    var pagingCookie = transformCookieForFetch(pagingArgs[1]);
    fetchXml = injectPagingArgsIntoFetchXml(fetchXml, pageNumber, pagingCookie);


    getWithFetchXml(entitySetName, fetchXml)        //recursion!
         .done(function(bodyValue){
             args[1].value = args[1].value.concat(jsonBody.value).concat(bodyValue);
             deferred.resolve(args);
        }
        .fail(function(error){
            deferred.reject(error);
        }));

    return deferred.promise;
}

function getWithFetchXml(entitySetName, fetchXml){
    return AuthProvider.retrieveToken()
        .then(createFetchOptions.bind(this, entitySetName, fetchXml))
        .then(addProxyToOptions)
        .then(executeRequest)
        .then(checkResponse)
        .then(tryReexecuteForNext5000Entries.bind(this, entitySetName, fetchXml))
        .then(onRequestSucceeded.bind(this, "successfully GET fetchXml against CRM"))
        .fail(onFail.bind(this, "webApiRequest.getWithFetchXml: failed to GET with fetchXml: \r\n"+fetchXml));
}

使用此设置Q会抛出“TypeError:无法读取属性'apply'of undefined”

应该按照以下方式工作:

  • 调用getWithFetchXml
  • 检索JsonObject List1和pagingtoken AND
  • ===调用getWithFechXml²
  • ===检索JsonObject List2和pagingToken AND
  • ======调用getWithFetchXml³
  • ======检索JsonObject List3 ,不带 pagingToken
  • ======返回列表
  • ===将List3追加到List2并将其返回
  • 将List2附加到List1并将其返回

1 个答案:

答案 0 :(得分:0)

这是一个愚蠢的语法错误...对不起,我没有在开发javascript时设置我的VisualStuidoCode以获得良好的反馈。

问题是:

getWithFetchXml(entitySetName, fetchXml)        //recursion!
        .done(function(bodyValue){
             args[1].value = jsonBody.value.concat(bodyValue);
             deferred.resolve(args);
        }
        .fail(function(error){
            deferred.reject(error);
        }));

错误的支架!应该是:

getWithFetchXml(entitySetName, fetchXml)        //recursion!
            .then(function(bodyValue){
                 args[1].value = jsonBody.value.concat(bodyValue);
                 deferred.resolve(args);
            })
            .fail(function(error){
                deferred.reject(error);
            });

这是我在Node.js中递归JavaScripts的运行代码:

function getWithFetchXml(entitySetName, fetchXml){
    return getWithFetchXmlRecursive(entitySetName, fetchXml, null);
}

var getWithFetchXmlRecursive = function(entitySetName, fetchXml, page){
    return AuthProvider.retrieveToken()
        (...)
        .then(tryReexecuteForNext5000Entries.bind(this, entitySetName, fetchXml, page))
        .then(onRequestSucceeded.bind(this, "successfully GET fetchXml against CRM"))
        .fail(onFail.bind(this, "webApiRequest.getWithFetchXml: failed to GET with fetchXml: \r\n"+fetchXml));
}

var tryReexecuteForNext5000Entries = function(entitySetName, fetchXml, page, args){
    (...)
    var fetchXmlPagingCookieResponse = jsonBody["@Microsoft.Dynamics.CRM.fetchxmlpagingcookie"];        
    var onRecursionSucceeded = function(bodyValue){
        args[1].value = jsonBody.value.concat(bodyValue);
        deferred.resolve(args);
    }
    var onRecursionFailed = function(error){deferred.reject(error);};

    if(!fetchXmlPagingCookieResponse){        
        deferred.resolve(args);
        return deferred.promise;
    }
    (...)
    console.log("webApiRequest.tryReexecuteForNext5000Entries: there is more! starting recursive request with depth: " + (page-1));
    getWithFetchXmlRecursive(entitySetName, fetchXml, ++page)        //recursion!
        .then(onRecursionSucceeded)
        .fail(onRecursionFailed);

    return deferred.promise;
}