-1

When looping through an array to find if the array contains a word that I am looking for, the loop always returns 'false' when if I console.log out the what is being compared I can clearly see that the word I am looking for (collectionNameLookingFor) is in the array (collectionNameArray) so it should return true.

function checkCollectionNames(arrayOfCollections, collectionName) {
  for (let i = 0; i < arrayofCollections.length; i++) {
    if (arrayOfCollections[i] === collectionName) {
      return true;
    }
  }
  return false;
}

function saveContentToDb(req, res) {
  const db = getDb();
  const pageDetails = req.body;
  let saveType;

  db.db(pageDetails.databaseName).listCollections().toArray((error, collections) => {
    if (error) {
      throw error;
    } else {
      collections.map(collection => (collection.name)).forEach(collectionNameArray => {
        const collectionNameLookingFor = req.body.page;
        const check = checkCollectionNames(collectionNameArray, collectionNameLookingFor);

        console.log('===========Looking to see if it is true or false==========');
        console.log(check);
        console.log(`Name of collection in Database: ${collectionNameArray} ::: ${collectionNameLookingFor}`);
        console.log('==========================================================');
        if (check === true) {
          saveType = 'updated';
          console.log(`saveType = ${saveType}`);
        } else {
          saveType = 'created';
          console.log(`saveType = ${saveType}`);
        }
      });
    }
  });
}
12
  • checkCollectionNames has a first parameter called arrayOfCollections, but you refer to it in the function itself as simply array. Surely these should match? Commented Mar 13, 2019 at 14:14
  • 5
    Have you considered using the Array.prototype.includes method instead of writing your own? Commented Mar 13, 2019 at 14:15
  • 2
    Should it be collectionName instead of page? Commented Mar 13, 2019 at 14:16
  • Can you please check that the elements of the array are string or object. use "console.log(typeof array[i])". Because you can create string in form of object using new String(...). Commented Mar 13, 2019 at 14:18
  • If the array elements are objects then use of "==" instead of "===" will do the work. Commented Mar 13, 2019 at 14:21

1 Answer 1

0

You might need to check against collectionName, because that is the parameter you hand over, beside arrayOfCollections, instead of the array itself.

function checkCollectionNames(arrayOfCollections, collectionName) {
    for (let i = 0; i < arrayOfCollections.length; i++) {
        if (arrayOfCollections[i] === collectionName) {
            return true;
        }
    }
    return false;
}

Short Version:

function checkCollectionNames(arrayOfCollections, collectionName) {
    return arrayOfCollections.includes(collectionName);
}
Sign up to request clarification or add additional context in comments.

3 Comments

That was a mistake when copy over to into the question. I've edited the question to fix this.
When logging out there are 5 values in the array, and the one that matches does return true, e.g. saveType = 'updated'. But the once it returns true it doesn't break, so the loop keeps going and ends up returning false, e.g. saveType = 'created' because the final value is False.
are you sure, that the collection contains strings and the wanted value is a string, too?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.