编辑: 是的,从技术上讲,这是一个较高级别的问题(为什么X不在循环之外时为何不填充X-异步问题等),但是我问的“真正”问题是:“什么是在我的特定情况下异步发生?”对于那些将来会看到这种情况的人,行读取器包使用异步方法,这是我不知道的事实。 “ forEach”循环也是如此-“ for of”循环是同步的,因此这是替代方法。否则,请使用async / await。 编辑结束
出于上下文考虑,我的问题之前有一些背景:
我有一个文件,其中包含SQL数据库的一堆输出。输出是通过几个JOIN语句生成的,因此所有输出都可以一次完成。数据来自三个表,其中的列由AS语句重命名。因此,如果该列来自表A,则将其命名为“ tableA_someColumnName”,如果来自表B,则将其命名为“ tableB_someOtherColumnName”,依此类推。这些表是订单,患者和帐户(疗养院)。每个病人一个记录,每个疗养院一个记录,订单多个记录。因此,不同订单的行中可能具有相同的疗养院和患者信息,例如:
patients_name, accounts_name, orders_order_no
Bob Bobbins, Shady Oaks Retirement, 12345
Bob Bobbins, Shady Oaks Retirement, 54321
Bob Bobbins, Shady Oaks Retirement, 98765
我正在使用Node.js逐行读取SQL输出(使用line-reader),并将基于列前缀(订单集合中的订单,患者)的唯一值插入单独的MongoDB集合中在患者集合等中)。由于存在重复信息的问题,我可以将订单信息直接插入到MongoDB中,但是对于患者和客户,我需要将它们放入两个列表中,然后在遍历整个文件后将它们重复数据删除,这样我就只能在MongoDB中插入唯一的行。但是,由于某些原因,当我使用.push()
将条目(作为对象)添加到我实例化的数组中时,这些条目不会在行读取器使用的.eachLine
循环之外持久存在。我曾尝试将let更改为var和许多其他内容,但是每次以后打印阵列时,它都是空的。如果我将其打印在循环内,则数组中的对象在那里,并且当前对象附加到该对象。我确定这是一个范围界定的问题,但是我不知道发生了什么。
这是我的代码:
const lineReader = require('line-reader');
const mongoService = require('./mongo.service')
const args = process.argv.slice(2)
const file = args[0];
const db = "test"
//create function to iterate through file and insert entries into MongoDB
function readFileAndInsertInMongo(fileName, dbName) {
let lineNo = 0;
let colNums = 0;
let colHeaders = [];
let homesList = [];
let patientsList = [];
lineReader.eachLine(fileName, function (line, last) {
console.log("line number is " + lineNo)
//If lineNo is 0, it must be the header line - get the headers
if(lineNo === 0) {
colHeaders = line.split('\t');
colNums = colHeaders.length;
} else {
let ordersObject = {};
let patientsObject = {};
let homesObject = {};
//Split lines on tab character
lineVals = line.split('\t');
//For each column header, go through the column name,
//get rid of the prefix, and use that as the object key
//Put column key/value pairs together in 3 different sets of data
for(i=0;i<=colNums;i++) {
if(colHeaders[i] !== undefined) {
if(colHeaders[i].toString().match(/^claims_|^orders_/)) {
ordersObject[colHeaders[i].toString().replace(/^claims_|^orders_/,'')] = lineVals[i];
} else if (colHeaders[i].toString().match(/^patients_/)) {
patientsObject[colHeaders[i].toString().replace(/^patients_/,'')] = lineVals[i];
} else if (colHeaders[i].toString().match(/^accounts_/)) {
homesObject[colHeaders[i].toString().replace(/^accounts_/,'')] = lineVals[i];
}
}
}
//orders are unique, just push them to MongoDB
mongoService.addEntry(dbName, "orders", ordersObject);
//push homesObject to homesList so deduplication can happen later (that part's not coded yet)
homesList.push(homesObject);
//HERE IS WHERE I CAN SEE MY ARRAY
console.log(JSON.stringify(homesList))
//we want to do the same thing with patients that we did with homes - this presumably doesn't work either
patientsList.push(patientsObject);
}
//ON A RELATED NOTE, THIS ALSO ITERATES JUST FINE, WHICH IS ODD
lineNo = lineNo + 1;
});
//ALTHOUGH I DON'T SEE ANYTHING HERE WHATSOEVER
console.log(homesList)
}
//Call the function
readFileAndInsertInMongo(file, db)
谢谢。