-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}`);
        }
      });
    }
  });
}
Andrew
  • 33
  • 5
  • `checkCollectionNames` has a first parameter called `arrayOfCollections`, but you refer to it in the function itself as simply `array`. Surely these should match? – Robin Zigmond Mar 13 '19 at 14:14
  • 5
    Have you considered using the [`Array.prototype.includes`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes) method instead of writing your own? – Randy Casburn Mar 13 '19 at 14:15
  • 2
    Should it be `collectionName` instead of `page`? – adiga Mar 13 '19 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(...). – RK_15 Mar 13 '19 at 14:18
  • If the array elements are objects then use of "==" instead of "===" will do the work. – RK_15 Mar 13 '19 at 14:21
  • you still have `page` in it. where is it from? – Nina Scholz Mar 13 '19 at 14:22
  • In `const collectionNameLookingFor = req.body.page;` is `page` a string? and are all other values in the `collectionNameArray` array definitely strings? Using `===` is not going to see `0` and `'0'` as equal for example and `===` will be case sensitive as well so `'aaa'` is not equal to `'AAA'` either. – Nope Mar 13 '19 at 14:25
  • Both collectionNameLookingFor and the name within collectionNameArray are both strings. – Andrew Mar 13 '19 at 14:29
  • If you want to stop the loop when the method returns `true` then I wouldn't use `.map` but a simple loop with a `break;` instead. – Nope Mar 13 '19 at 14:39
  • Possible duplicate of [Break statement in javascript array map method](https://stackoverflow.com/questions/12260529/break-statement-in-javascript-array-map-method) – Nope Mar 13 '19 at 14:41
  • Based on your comment on the given answer below it seems you want the loop to break on match, which you can't do in a `.map` call but you can replace the loop with the one in the suggested duplicate I linked. that should hopefully sort it out. – Nope Mar 13 '19 at 14:42
  • @Nope You was right, the .map was an issues, so had to create my own loop function which also had to loop through an array contain json's to retrieve only the name values, which then could be easily used in an if statement to return true or false. – Andrew Mar 13 '19 at 15:53

1 Answers1

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);
}
Emma
  • 27,428
  • 11
  • 44
  • 69
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • That was a mistake when copy over to into the question. I've edited the question to fix this. – Andrew Mar 13 '19 at 14:20
  • 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. – Andrew Mar 13 '19 at 14:31
  • are you sure, that the collection contains strings and the wanted value is a string, too? – Nina Scholz Mar 13 '19 at 14:38