-1

I have this code, and I am trying to get the data from the function addrBlk to display in console through fs.writeFile. I have tried with this code with these params and this function to display

```let addrBlk = (name, address1, address2, city, state, zip) => {
return name + "<br>" + address1 + "<br>" + address2 + 
"<br>" + city + ", " + state + "  " + zip;
}```


```fs.writeFile("address.txt", addrBlk, (err) => {
if(err) {
    console.log(err);
}
else {
    console.log("Address written successfully, address is: ");
    console.log(fs.readFileSync("address.txt", "utf8"));
}
});```

and it returns exactly what the return says, I have also tried

```fs.writeFile("address.txt", `${name}`, (err) => {
if(err) {
    console.log(err);
}
else {
    console.log("Address written successfully, address is: ");
    console.log(fs.readFileSync("address.txt", "utf8"));
}
});```

and It returns an error saying name not defined. How can I return this data to a txt file with these params and return message??

Dylan T
  • 139
  • 11
  • can you try to change `fs.writeFile("address.txt", addrBlk, (err) => {` to `fs.writeFile("address.txt", addrBlk(), (err) => {` – kennarddh Jan 24 '23 at 03:27
  • One step closer! not sure why I didnt think of using the parentheses for function params. Thank you. Now I am just getting undefined for the params, which is good. Just need to find a way to get the input. – Dylan T Jan 24 '23 at 03:30
  • Oh, and a way to render the html
    through the console. I guess I could use js \n.
    – Dylan T Jan 24 '23 at 03:31
  • You need to call this with the params `addrBlk('name', 'address1', 'address2', 'city', 'state', 'zip')` and change `
    ` to `\n`.
    – kennarddh Jan 24 '23 at 03:36
  • i get same output with no quotes. What I need is a way to do input to dynamically display the params. – Dylan T Jan 24 '23 at 03:45
  • You want to input from cli so user can type `address: user address`? – kennarddh Jan 24 '23 at 03:46
  • Yes. Not sure what node methods I need to use to implement the cli input. A lot of tutorials were pretty scattered. – Dylan T Jan 24 '23 at 03:48
  • Are you using esm or common js? `require` or `import`? What is your nodejs version? – kennarddh Jan 24 '23 at 03:53
  • I can use any node version. I prefer to use verson 10.14.0 or any tens. But I can still use later versions. And im using es6 at highest. – Dylan T Jan 24 '23 at 03:56
  • I assume you are using require and without promise. – kennarddh Jan 24 '23 at 03:58

1 Answers1

1
const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});


const fileName = 'address.txt'

const formatInput = (name, address1, address2, city, state, zip) => `${name}\n${address1}\n${address2}\n${city}, ${state} ${zip}`;

rl.question('Name: ', (name) => {
  rl.question('Address 1: ', (address1) => {
    rl.question('Address 2: ', (address2) => {
      rl.question('City: ', (city) => {
        rl.question('State: ', (state) => {
          rl.question('Zip: ', (zip) => {
            const data = formatInput(name, address1, address2, city, state, zip)

            fs.writeFile(fileName, data, (err) => {
              if (err) {
                console.log(err);
              } else {
                console.log("Address written successfully, address is: ");
                console.log(fs.readFileSync(fileName, "utf8"));
              }
            })
            rl.close();
          });
        });
      });
    });
  });
});
kennarddh
  • 2,186
  • 2
  • 6
  • 21
  • This is it. Thank you. The readLine and createInterface modules are exactly what I needed. Man, so many awesome node features. – Dylan T Jan 24 '23 at 04:24