So I have an array variable called 'fileArray' that contains objects that look like this:
{
file: 'lesson 2',
id: '',
description: undefined,
type: 'application/vnd.google-apps.document',
properties: { subject: [], grade: [], industry: [], imgsrc: '' },
parents: [ '' ]
}
I am trying to detect if the id of a certain array already exists within a MongoDB database. If it does, then push the subject, grade, and industry values that exists within the database, and update the original fileArray file to have those values. If something exists in the fileArray and doesn't currently have an entry that matches it's id, then an entry will be created.
The problem is whenever I search through the database for a specific entry within the for loop I have, the mongoose countDocuments function gets caught on one specific array in the fileArray, and copies only that object by the number of entries in the fileArray. So I get say 46 entries that all look exactly the same, with the same id. I know this probably has something to do with asynchronous functions from the research that I've done, but I'm not well versed in using those enough to know what exactly to do. How do I get this work properly?
The schema looks like this:
var tagSchema = new Schema ({
id: String,
subject: Array,
grade: Array,
industry: Array
})
const TagFile = mongoose.model('TagFile', tagSchema)
And the for loop code I'm using looks like this:
for(var k = 0; k < fileArray.length; k++) {
var counted = fileArray[k];
console.log(counted);
TagFile.countDocuments({id: fileArray[k].id}, (err, count) => {
if(err) return console.log(err);
//console.log(count);
//console.log(count + " is the number of results")
// console.log(counted)
if(count === 0) {
//ALL
if(counted.properties.subject === [] && counted.properties.grade === [] && counted.properties.industry === []) {
var newTags = new TagFile({id: counted.id});
newTags.save((err, res) => {
if (err) return console.log(err);
//console.log(res);
})
}
if(!counted.properties.subject != [] && !counted.properties.grade != [] && !counted.properties.industry != []) {
var newTags = new TagFile({id: counted.id, subject: [counted.properties.subject], grade: [counted.properties.grade], industry: [counted.properties.industry] })
newTags.save((err, res) => {
if (err) return console.log(err);
//console.log(res);
})
fileArray.push({subject: counted.properties.subject, grade: counted.properties.grade, industry: counted.industry})
}
//SUBJECT
if(counted.properties.subject != [] && counted.properties.grade === [] && counted.properties.industry === []) {
var newTags = new TagFile({id: counted.id, subject: [counted.properties.subject]})
newTags.save((err, res) => {
if (err) return console.log(err);
//console.log(res);
})
fileArray.push({subject: counted.properties.subject})
}
if(counted.properties.subject != [] && counted.properties.grade != [] && counted.properties.industry === []) {
var newTags = new TagFile({id: counted.id, subject: [counted.properties.subject], grade: [counted.properties.grade]})
newTags.save((err, res) => {
if (err) return console.log(err);
//console.log(res);
})
fileArray.push({subject: counted.properties.subject, grade: counted.properties.grade})
}
if(counted.properties.subject != [] && counted.properties.grade === [] && counted.properties.industry != []) {
var newTags = new TagFile({id: counted.id, subject: [counted.properties.subject], industry: [counted.properties.industry]})
newTags.save((err, res) => {
if (err) return console.log(err);
//console.log(res);
})
fileArray.push({subject: counted.properties.subject, industry: counted.industry})
}
//GRADE
if(counted.properties.subject === [] && counted.properties.grade != [] && counted.properties.industry === []) {
var newTags = new TagFile({id: counted.id, grade: counted.grade})
newTags.save((err, res) => {
if (err) return console.log(err);
//console.log(res);
})
fileArray.push({grade: counted.properties.grade})
}
if(counted.properties.subject === [] && counted.properties.grade != [] && counted.properties.industry != []) {
var newTags = new TagFile({id: counted.id, grade: counted.grade, industry: counted.industry})
newTags.save((err, res) => {
if (err) return console.log(err);
//console.log(res);
})
fileArray.push({grade: counted.properties.grade, industry: counted.industry})
}
//INDUSTRY
if(counted.properties.subject === [] && counted.properties.grade === [] && counted.properties.industry != []) {
var newTags = new TagFile({id: counted.id, industry: counted.industry})
newTags.save((err, res) => {
if (err) return console.log(err);
//console.log(res);
})
fileArray.push({industry: counted.industry})
}
}
if(count > 1) {
TagFile.find({id: counted.id}, (err, res) => {
if(err) return console.log(err);
//console.log(res);
//fileArray.push({subject: res[0].subject, grade: res[0].grade, industry: res[0].industry})
})
}
else {
return;
}
})
}