skip to Main Content

I’m creating a "Search File" page that lets users enter a string to find some local files (with NodeJS). I want it so the user can type a part of the string and still get results. For example, if they only typed "ha" then all files that contain the string "ha" should be displayed.

Here’s my current code:

app.post("/search", (req, res) => {
  //to read all the file names in the directory, returns ["file_01.txt", "file_02.txt"]
  const fileNames = fs.readdirSync(__dirname)
  
  const userInput = req.body.userInput;
  const subArr = fileNames.filter((str) => str.includes(userInput));
  console.log(subArr); //returns empty
  res.render("search.ejs");
});

However, if I declare it explicitly:

const abc = ["ab", "bc", "cd"];
const subArr = abc.filter((str) => str.includes(userInput));
console.log(subArr) //returns an array of words that contain the user input, just like I want it to

Why can’t I filter from fileNames but it works with abc? Please help, thank you!

2

Answers


  1. It looks like the filtering is working fine considering that it works with abc…
    In my past experience the issue with includes method is that it is case sensitive and i was trying to debug for so long
    try to convert both strings to lowercase or uppercase:

    const subArr = fileNames.filter((str) => 
    str.toLowerCase().includes(userInput.toLowerCase()));
    

    also make sure that everything works as expected some might not like it but console.log is my best friend:

    app.post("/search", (req, res) => {
        const fileNames = fs.readdirSync(__dirname);
        console.log("Directory:", __dirname); // Debug
        console.log("Files in directory:", fileNames); // Debug
    
        const userInput = req.body.userInput;
        console.log("User Input:", userInput); // Debug
    
        // Case-insensitive filtering
        const subArr = fileNames.filter((str) => 
        str.toLowerCase().includes(userInput.toLowerCase()));
    
        console.log("Filtered Files:", subArr); // Debug
    
        res.render("search.ejs", { files: subArr }); // Ensure you're passing 
        the filtered list to your template
    });
    

    Check where the is not doing what it is supposed to do so you can pinpoint the issue
    Make sure __dirname is the directory that you looking for
    Make sure that fileNames contains files of the said directory and so on…

    Login or Signup to reply.
    • Make sure that req.body.userInput is populated correctly. If you are
      using a form to submit the search query, ensure that the form is
      configured to send the data properly.
    • Case sensitivity: String.includes() is case-sensitive. If you want
      case-insensitive matching, you might want to convert both the file
      names and the user input to lowercase (or uppercase) before

    app.post("/search", (req, res) => { const fileNames = fs.readdirSync(__dirname); const userInput = req.body.userInput.toLowerCase(); // Convert to lowercase for case-insensitive matching const subArr = fileNames.filter((str) => str.toLowerCase().includes(userInput)); console.log(subArr); res.render("search.ejs", { results: subArr, userInput: userInput }); });

    req.body.userInput.toLowerCase()

    ensures that the search is
    case-insensitive.

    str.toLowerCase().includes(userInput)

    converts both the file names
    and the user input to lowercase for consistent comparison

    .

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search