-3

I tried to create an terminal like interface using javascript. (http://whois.ane.sh/terminal.html)

But I tried to call a function which I already created, it says undefined function.

This is my main logic of encode function And if I alert the final it works fine as expected but return is not returning the same value.

    fetch('https://anish.vip/secure/encdec.php', {
          method: 'POST',
          body: formData
      })
      .then(response => response.text())
      .then(data => {
          let final = JSON.parse(data).final;
          //alert(final);
          return final;

      })
      .catch(error => {
          return error;
      });

Here's how the function is being called

if(keepValue.substring(0, 6) == 'encode' || keepValue.substring(0, 6) == 'decode'){
    encode(keepValue)
    .then(returnedValue => {
        // Use the returned value in a subsequent part of your code
        let systemOutput = returnedValue; // Assign the value
        outputField[outputField.length - 1].innerHTML = systemOutput;
        initialSection.innerHTML += "<br />" + initialPrompt;
    })
}

I created an function encode. When user enters 'encode "message" pass' in the terminal, it is supposed to fetch data from my another php file and display the output. However it displayed undefined for my several attempts and after I did what chatgpt said, it says function undefined. Please help me out.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • You didn't provide the entire code of the first function, but it looks like it doesn't **return** the promise. – trincot Aug 04 '23 at 19:25
  • 1
    Change `fetch(...)` to `return fetch(...)` – Barmar Aug 04 '23 at 19:27
  • 2
    Also it doesn't really make sense to use `response.text()` and then `JSON.parse()`. You should just use `response.json()`. – Mushroomator Aug 04 '23 at 19:29
  • @trincot the entire code can be found on the link I mentioned. But I think the problem is in the part I wrote here. – Anish Khatri Aug 04 '23 at 19:41
  • @Barmar it worked! Thanks for the help. But little problem though, I changed the fetch to return fetch and rest of the job is done in the second part. Here's what it look like now. ```if(....){ encode(keepValue).then(response => response.text()).then(data => { let final = JSON.parse(data).final; let systemOutput = final; });} inputField = document.querySelector("#userInput"); outputField = document.querySelectorAll(".systemOutput"); ``` Now it says the inputField is undefined. – Anish Khatri Aug 04 '23 at 19:46
  • no links to outside website plz, if you gonna post a code, create a snippet with the tools provided by SO – Chris G Aug 04 '23 at 19:57
  • It's impossible to read long code in comments. If you have a new problem, post a new question and format the code readably. – Barmar Aug 04 '23 at 20:01
  • I don't see where you're even using `inputField` – Barmar Aug 04 '23 at 20:01

0 Answers0