我试图在一个循环中使用axios从localhost调用API,问题是有时它工作正常,但大多数错误发生。
cause: Error: connect ECONNREFUSED ::1:8000
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16) {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '::1',
port: 8000
}
字符串
或者这个错误
cause: Error: connect ETIMEDOUT xx:xxxx::xxx:xxxx:xxx
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16) {
errno: -4039,
code: 'ETIMEDOUT',
syscall: 'connect',
address: 'xx:xxxx::xxx:xxxx:xxx',
port: 443
}
型
向此终结点发出请求:
app.get("/initialize-db", async (req, res) => {
const response = await axios
.get("http://localhost:8000/symbol-list")
.catch(errorHandler);
const recordCoiCexUrl = "http://localhost:8000/record-coi-cex";
const symList = response.data;
const requests = symList.map((element) => {
axios.get(`${recordCoiCexUrl}/${element}`);
});
await axios.all(requests).catch(errorHandler);
res.send("Initialize the database");
});
型
endpoint /record-coi-cex是这样的:
app.get("/record-coi-cex/:symbol", async (req, res) => {
const symbol = req.params.symbol;
const url = "https://webapi.niftytrader.in/webapi/option/fatch-option-chain";
const response = await axios
.get(url, {
params: { symbol: symbol },
})
.catch(errorHandler);
//* creating mongoose model with collection name same as symbol
const Symbol = mongoose.model(symbol, oiDataSchema, symbol);
//* parsing response from api to get the option data
const opDatas = response.data.resultData.opDatas;
//* important vaiables required
const spot = opDatas[0].index_close;
opDatas.sort((a, b) => {
return a.strike_price < b.strike_price;
});
const atmIndex = opDatas.findIndex((obj) => obj.strike_price > spot);
let total_puts_change_oi = 0;
let total_calls_change_oi = 0;
//* entry of all sp lying in range of 1000 points up and down
const start = Math.max(0, atmIndex - 20);
const end = Math.min(opDatas.length - 1, atmIndex + 20);
for (let i = start; i <= end; i++) {
const element = opDatas[i];
const sp = element.strike_price;
//* suming up coi of 10 strikes up-down
if (Math.abs(i - atmIndex) <= 10) {
total_puts_change_oi += element.puts_change_oi;
total_calls_change_oi += element.calls_change_oi;
}
//* storing coi of 20 strikes up-down in db
const doc = await Symbol.findOne({ strikePrice: sp }).catch(errorHandler);
const oiArray = {
putsCoi: element.puts_change_oi,
callsCoi: element.calls_change_oi,
};
if (!doc) {
await Symbol.create({ strikePrice: sp, oiArray }).catch(errorHandler);
} else {
await Symbol.updateOne({ strikePrice: sp }, { $push: { oiArray } }).catch(
errorHandler
);
}
}
型
当我尝试为一个端点进行API调用时,它工作正常,有时在循环内也工作正常,但错误的原因是什么?如何克服这个问题?
1条答案
按热度按时间7tofc5zh1#
这会导致所有符号按顺序而不是并行提取:
字符串
请注意,这将更慢。如果速度不能接受,你可以看看一些允许 * 一些 * 并行化的库,但我可能不会超过6。