我在使用MongoDB和MySQL的节点js中创建货币汇率网站后端,
首先,我将数据从API存储到MongoDB中,然后为了节省RAM和CPU,我决定在MySQL中创建和存储分析数据。
请查看以下代码,
update24HRateArr: () => {
var da = [];
db.query('TRUNCATE TABLE `rate24h`', (xerr, xres) => {
if (xerr) console.log(xerr.message);
});
mdb.currency((currList) => {
if (currList && currList.length > 0) {
currList.length = 50;
for (var c = 0; c < currList.length; c++) {
mdb.rateArr(currList[c], '24h', (data) => {
if (data && data.length > 0) {
for (var i = 0; i < data.length; i++) {
var tmp = [];
tmp.push(data[i].id);//1->id
tmp.push(i);//2->number
tmp.push(data[i].time);//3->time value
tmp.push(data[i].price_usd);//4->price usd
tmp.push(data[i].price_btc);//5->price btc
tmp.push(data[i].price_inr);//6->price inr
tmp.push(fn.time.unix());//7->DB update time
da.push(tmp);
}
//main area for done call
/*
if (da.length == 1200) {
db.query('INSERT INTO `rate24h` VALUES ?', [da], (err, datax) => {
if (err) console.log(err);
else {
console.log(`${fn.time.timeStamp()} Data(${datax.affectedRows}) Inserted for Rate24H`);
}
});
console.log(da);
}*/
console.log(`inside i ${da.length}`);
}
console.log(`inside iRateArr ${da.length}`);
});
//console.log(`inside c ${da.length}`); <- always 0 no way
}
}
else {
console.log('Currency Loading failed');
}
});
},
现在,我知道da var的长度在这种情况下将是1200,因为50种货币和24小时但在某些情况下它是灵活的,
我无法准确确定何时通过for循环完成此 da var 更新,以便我可以对mysql执行插入查询
如果(da.length === 1200)不够,我需要确切的点,我可以执行INSERT查询。
谢谢