Currently, I am reading through user records in a file line by line. Each line represents a user record that I create if it doesn't already exist. It's possible for the same user record to be spread across multiple lines, so when I detect that it has already been created, I simply skip over it.
The code snippet I'm using is shown below:
async onFilesAdded(files: FileList){
this.fileToUpload = files.item(0);
let fileReader = new FileReader();
fileReader.onload = async (e) => {
this.showProgress = true
var lines = fileReader.result.toString().split(/[\r\n]+/g); // handling both Windows and Unix linebreaks
this.totalLines = lines.length
var firstLine = false
this.dcSvc.getPageImages().then(
(resp) => {
console.log("resp in getPageImage" + JSON.stringify(resp))
this.pageMap = resp
this.lineCount = 0
for(let line of lines){
if(firstLine == false){
firstLine = true
}else{
this.createClickHistory(line).then(
(resp)=> console.log("line processed")
)
}
}
}
)
}
fileReader.readAsText(this.fileToUpload);
}
async createClickHistory(line:string){
var lineArr = line.split(',')
const userName = lineArr[1]
this.dcSvc.getUser(userName).then(
(res:any) => {
console.log("Response of get user is:::" + JSON.stringify(res))
if(res.length == 0 ){
//user does not exist in the DB
console.log("user:" + userName + " does not exist so creating it")
const userPayload = {
"userName": userName
}
this.dcSvc.createUser(userName, userPayload).then((rsp) => {})
}else{
//user exists so skip
}
})
}
createUser(userName:string, userPayload){
return this.db.object("/users/" + userName).set(userPayload)
}
getUser(userName:string){
return new Promise((resolve, reject) => {
this.db.list('/users',
ref => ref.orderByChild("userName").equalTo(userName)
).snapshotChanges().subscribe(
(res) => {
resolve(res)
}
)
})
}
It seems that the current code doesn't wait for each line to process before moving on to the next one. As a result, I have to run the code multiple times to ensure all data is imported correctly.