Node.js 使用 q 框架递归调用多个 GET 请求
Node.js recursive calls with q framework for multiple GET requests
在搜索 Whosebug 和 Q 文档后,不幸的是我仍然不理解 q.ninvoke 函数。我想知道是否有人可以向我解释它如何适用于我的场景:
我正在获取最多 returns 5000 个条目的 fetchXml 请求的结果。我想实现一个调用自身(通过链)的方法,直到服务器不再响应 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: Cannot read property 'apply' of undefined"
应该如下工作:
- 调用 getWithFetchXml
- 检索 JsonObject List1 和 pagingtoken AND
- ===调用 getWithFechXml²
- ===检索 JsonObject List2 和 pagingToken AND
- ======调用 getWithFetchXml³
- ======检索 JsonObject List3 没有 pagingToken
- ======returns 列表
- ===将 List3 附加到 List2 并且 returns 它
- 将 List2 附加到 List1 并且 returns 它
这是一个愚蠢的语法错误...对此感到抱歉,我在开发 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 中的 运行 递归 JavaScript 代码:
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;
}
在搜索 Whosebug 和 Q 文档后,不幸的是我仍然不理解 q.ninvoke 函数。我想知道是否有人可以向我解释它如何适用于我的场景:
我正在获取最多 returns 5000 个条目的 fetchXml 请求的结果。我想实现一个调用自身(通过链)的方法,直到服务器不再响应 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: Cannot read property 'apply' of undefined"
应该如下工作:
- 调用 getWithFetchXml
- 检索 JsonObject List1 和 pagingtoken AND
- ===调用 getWithFechXml²
- ===检索 JsonObject List2 和 pagingToken AND
- ======调用 getWithFetchXml³
- ======检索 JsonObject List3 没有 pagingToken
- ======returns 列表
- ===将 List3 附加到 List2 并且 returns 它
- 将 List2 附加到 List1 并且 returns 它
这是一个愚蠢的语法错误...对此感到抱歉,我在开发 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 中的 运行 递归 JavaScript 代码:
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;
}