skip to Main Content

I want to see on page all text that is saved in database, but i want it to be like a list of text example

  • That the first text from data base
  • Here is the second one.

I try like this:

app.get('/readtext', async (req, res) => {
  try {
    const document = await client.query("SELECT text FROM document");
    let variable = document.rows.map(row => row.text).join("n");
    res.send(variable);
  } catch (error) {
    console.log(error);
  }
});

and my output is:
That the first text from data base Here is the second one.
i went it to be like this
That the first text from data base

  • Here is the second one.
    with or wihtout "*" the ideea is any line from database be on a new line

i use node.js

2

Answers


  1. On a web page you need to use br tag instead of just n:

    app.get('/readtext', async (req, res) => {
      try {
        const document = await client.query("SELECT text FROM document");
        let variable = document.rows.map(row => row.text).join("<br>");
        res.send(variable);
      } catch (error) {
        console.log(error);
      }
    });
    
    Login or Signup to reply.
  2. The easier way would be to just send an array where each element is the text of each row. In your code that would be:

    app.get('/readtext', async (req, res) => {
      try {
        const textArray = [];
        const document = await client.query("SELECT text FROM document");
        document.rows.map(row => {
          textArray.push(row.text);
        });
        res.send(textArray);
      } catch (error) {
        console.log(error);
      }
    });
    

    Of course you will need to make tha right adjustments to the front end.

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