0

Everything works just fine I'm just new to js and web programming and non-experienced with async functions. I had tried with Promises with then() and couldn't do it. I'm trying to pass the data isbn number to data1 object

let isbn = "";
  //
  req.files.imageFile.mv(imageAddress, function (error) {
    if (error) {
      console.log("Couldn't upload the isbn image file.");
      console.log(error);
    } else {
      console.log("Image file successfully uploaded!");
      readText(imageAddress)
        .then(isbnNumber => {

        }).catch()

    }
  });

  var data1 = {
    bookName,
    isbn,
  };

From this async function that recognize text and parse it as i want

async function readText ( imageAddress ) {
      await worker.load();
      await worker.loadLanguage("eng");
      await worker.initialize("eng");
      const {
        data: { text },
      } = await worker.recognize( imageAddress );
      console.log(text);
      await worker.terminate();
      //get the isbn number from readed text
      let textArr = text.split("\n");
      var isbnText = "";
      var i;

      for(i= 0; i < textArr.length; i++){
        var str = textArr[i];
        if (str.includes("ISBN")){
          isbnText = textArr[i];
        }
      }
      isbnText = isbnText.replace("ISBN", "");
      let isbnNumber = isbnText.replace(/-/g, "");
      isbnNumber = isbnNumber.replace(/\D/g, '')
      console.log(isbnNumber);
      return isbnNumber;

    }

I want to equelize to isbn which is declared outside of the function

 readText(imageAddress)
        .then(isbnNumber => {
            isbn = isbnNumber;
        }).catch()
  • Also relevant: [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/q/23667086) – VLAZ Apr 08 '20 at 13:54
  • 1
    Put the `var data1 = {…}` inside the `then` callback, or use `await` there as well. – Bergi Apr 08 '20 at 13:54
  • From what i understand, in then() function you cant change the original value – Mahmut Yuncu Apr 08 '20 at 13:55
  • Hi i did this readText(imageAddress) .then((isbnNumber) => { var data1 = { bookName, isbn, }; }) .catch(); then i got ReferenceError: data1 is not defined – Mahmut Yuncu Apr 08 '20 at 14:03

0 Answers0