1

So I have this function I am calling outside of my app.js, it's another JavaScript file

const Lodash = require("lodash")
module.exports.postExist = function(posts, req) {
  let postValue = []
  posts.forEach(post => {
      if (Lodash.lowerCase(req.params.postName.toLowerCase()) === Lodash.lowerCase(post.title.toLowerCase())) {
        postValue = post;
        console.log(postValue + "inside");
        return true;
      }
    }
  );

  console.log("outside");
  return false
}

Console log

[object Object]inside
outside

The return value of this function is giving me false which I don't understand because the if statement is executed at some point from what I'm getting from the console log [object Object]inside, why isn't my function returning true and exiting the function. Why is it continuing the execution to my other return value?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122

0 Answers0