在我的代码中,我做了一些API调用,有时其中一个返回错误,但如果我在它工作之后重新执行它(我真的不知道什么是错的,也许我发布的JSON没有完成或者我没有知道...)
所以,为了使我编码:
HTTP.call("POST", "http://localhost:3000/api/dashboards/db", { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': APIKEY, }, data: { dashboard: dataJSON, overwrite: true } }, function(error, result) { if (!error) { console.error("result post dataJSON --------------------OK------------") } else { console.log("error post dataJSON --------------------KO------------") HTTP.call("POST", "http://localhost:3000/api/dashboards/db", { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': APIKEY, }, data: { dashboard: dataJSON, overwrite: true } }, function(error, result) { if (!error) { console.error("result post dataJSON at the 2nd time --------------------OK------------") } else { console.log("error post dataJSON at the 2nd time --------------------KO------------") } }); }但是制作这样的东西非常难看我认为这是一种在出现错误时回忆代码部分的方法吗?
In my code I make some API calls and sometimes 1 of them return an error but if I re-do it just after it works (I don't really know what's wrong maybe the JSON that I post is not finish or I don't know...)
So to make that I have coded:
HTTP.call("POST", "http://localhost:3000/api/dashboards/db", { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': APIKEY, }, data: { dashboard: dataJSON, overwrite: true } }, function(error, result) { if (!error) { console.error("result post dataJSON --------------------OK------------") } else { console.log("error post dataJSON --------------------KO------------") HTTP.call("POST", "http://localhost:3000/api/dashboards/db", { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': APIKEY, }, data: { dashboard: dataJSON, overwrite: true } }, function(error, result) { if (!error) { console.error("result post dataJSON at the 2nd time --------------------OK------------") } else { console.log("error post dataJSON at the 2nd time --------------------KO------------") } }); }But it's very ugly to make something like this I think so is it a way to recall the code's part when there is an error ?
最满意答案
好吧,我的第一个建议就是要了解为什么第一次呼叫失败,看看你是否可以解决它。
除此之外,它取决于您计划使用数据等,但是,例如,您可以在函数中隔离HTTP调用,并使用相同的参数调用该函数。
function callAPIwithData(myData) { HTTP.call("POST", "http://localhost:3000/api/dashboards/db", { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': APIKEY, }, data: myData }, function(error, result) { if (!error) { console.error("OK: result post dataJSON") } else { console.log("KO: error post dataJSON") console.log("retrying ...") callAPIWithData(myData); } }); }当然,这将需要一组检查和平衡,所以它不会卡在重新尝试无限的casa有其他东西被打破,但你明白了。
Well, my first suggestion is to understand why the call fails the first time around and see if you can fix it.
Excluding that, it depends a bit on you plan to use the data and such, but, for example, you could isolate the HTTP call in a function, and recall the function with the same arguments.
function callAPIwithData(myData) { HTTP.call("POST", "http://localhost:3000/api/dashboards/db", { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': APIKEY, }, data: myData }, function(error, result) { if (!error) { console.error("OK: result post dataJSON") } else { console.log("KO: error post dataJSON") console.log("retrying ...") callAPIWithData(myData); } }); }of course this will need a set of checks and balances so it won't get stuck re-trying to infinity in casa there's something else broken, but you get the idea.
更多推荐
发布评论