0

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;
          }
        })
      }
Matt.G
  • 171
  • 2
  • 7
  • You have only one `counted` variable shared by all the asynchronous callbacks. – Bergi Jan 17 '21 at 23:06
  • If you want a simple solution, make all asynchronous calls sequential by using `async`/`await`. – Bergi Jan 17 '21 at 23:07
  • Thank you for the response, @Bergi Where exactly would I would add the await calls? I can't put them on if statements. Sorry if this is super simple, I'm still trying to learn how to use async properly. :) – Matt.G Jan 17 '21 at 23:19
  • Yes you can put them inside `if` statements, that's why `async`/`await` is so great! You will need to use mongoose methods in promise style though - don't pass a callback to any of them. – Bergi Jan 17 '21 at 23:37

0 Answers0