我正在使用node.js,当我运行我的代码时,有时它运行正常,然后我经常在下面得到这个错误,这似乎是数组的一个问题(未定义弹出的随机部分数组,有时它接近console.log的末尾,其他时候(如下所示)它在最开始。通过WebSocket不断更新图表/ +符号+ .json,这可能导致错误,如果文件我正在尝试阅读时正在更新过程中是否还有另外一种解决方法?能够读取当前正在更新的文件吗?
POABTC rsi 52.63
ZILBTC rsi 48.63
undefined:1
SyntaxError: Unexpected end of JSON input
我似乎无法弄清楚为什么?看到有时它会运行良好?我的代码如下:
setInterval(checkPairs, 300000);
checkPairs();
function checkPairs() {
var RSI = require('technicalindicators').RSI;
const fs = require('fs');
var Twit = require('twit');
var config = require('./tconfig');
var T = new Twit(config);
var message = [];
var theLoop = ['LTCBTC', 'LTCETH'];
for (i = 0; i < theLoop.length; i++) {
var symbol = theLoop[i];
var data = fs.readFileSync('charts/' + symbol + '.json');
var CurrentPair = JSON.parse(data);
var prices = [];
var period = 14;
var lowTotal = [];
var closeTotal = [];
var rsiArray = [];
for (var item in CurrentPair) {
prices.push(Number(CurrentPair[item].close));
}
var newRSI = [];
newRSI = RSI.calculate({
period: period,
values: prices
});
var rsiResult = newRSI[newRSI.length - 1];
console.log(symbol + " rsi " + rsiResult);
if (rsiResult <= 23) {
var twitMessage = "RSI is LOW (" + rsiResult + ") ";
var splitSymbol = "";
if (symbol.includes("BTC", 3) || symbol.includes("BTC", 4) ||
symbol.includes("BTC", 5) || symbol.includes("BTC", 6)) {
splitSymbol = symbol.replace("BTC", "");
};
}
}
}
.json文件的样本
[
{
"open": "0.21320000",
"high": "0.21320000",
"low": "0.21320000",
"close": "0.21320000",
"volume": "0.00000000",
"timestamp": 1522180500000
},
{
"open": "0.21320000",
"high": "0.21320000",
"low": "0.21320000",
"close": "0.21320000",
"volume": "0.00000000",
"timestamp": 1522180800000
},
任何帮助将不胜感激,谢谢。