Couldn't send variable to function


#1

This code find file names in text file, and then trying to find files by name in specified directory. files name is array of strings. For example

nameArr in console [“Реклама - Макси дек_скидки0514.mp3”, “Реклама - Краснодар Центр Города и Новый Горизонт.mp3”]

nameArr[0] in colsole "Реклама - Макси дек_скидки0514.mp3"

trying to find files

find.fileSync(nameArr[0], ‘E:/transfer’) as result [] - empty array

when I give arg “manually” all work as expected

find.fileSync(“Реклама - Макси дек_скидки0514.mp3”, ‘E:/transfer’) as result [“E:\transfer*Реклама - Макси дек_скидки0514.mp3*”]

so I can’t understand, why function doesn’t work with nameArr[0]

code examle

const searcher = require('string-search');
const path = require('path');
const fs = require('fs');
const find = require('find');

let openButton = document.getElementById('openButton');
let openInput = document.getElementById('openInput');
let nameArrayButton = document.getElementById('nameArray')
let nameArr = []; //names of the files


nameArrayButton.addEventListener('click', ()=>{
  find.fileSync(nameArr[0], "E:/transfer");
})

openButton.addEventListener('click', () => {
  openInput.click()
});

openInput.addEventListener('change', handleFileSelect);

function handleFileSelect(e) {
  let file = e.target.files[0];
  let reader = new FileReader();

  reader.onload = function (e) {//parsing the text file and find names on it
    string = e.target.result;
    searcher.find(string, "Реклама")
  .then(result => arrayRebuilder(result))
  .then(result => nameReturn(result))
  }
  reader.readAsText(file, "windows-1251");
}

function arrayRebuilder(array) {
  let newArray = [];
  for (let i = 0; i < array.length; i++) {
    let editFilePath = array[i].text.slice(9, array[i].length);

    newArray.push(editFilePath);
  }
  return newArray;
}

function nameReturn(result) {
  result.forEach(function(item) {
    let pt = path.parse(item).base;
    nameArr.push(pt)
  })
}

#2

Can you please edit you post (or make a new comment below) that has the code, but marked properly (either click </> or wrap it in triple backticks; you’ll see if it worked by looking at the preview)


#3

Sorry, I’ve done


#4

At this point I’d make sure to call openDevTools() on your main window and drop a break point on nameReturn. The easiest way for your nameArr array to come up empty is for there to be nothing in the argument passed to nameReturn. Work backwards from there.