Question

How to put a result.json() in a variable

Posted August 2, 2021 151 views
Node.jsJavaScript

Hi!
I’m doing this tutorial in a web page ([https://www.digitalocean.com/community/tutorials/how-to-use-ejs-to-template-your-node-application-es](http://)), but I have to do it with a postgreSQL result, an async function (the “await” is already inside the function).

export const getPais = async (req:Request,res:Response): Promise<Response>=> {
try{
const response: QueryResult = await pool.query(‘SELECT * FROM paises’);

    return res.status(200).json(response.rows);

}catch(e){
    console.log(e);
    return res.status(500).json('Error Interno');
}
}

¿How can I put it in a variable?

Thank you.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hello there,

I think that if you save the variable with the await keyword it will work. It should look something like this:

const data = await(result.json)

Let me know if this works.

Regards,
Boyan

  • Yes, but that would be inside the function, but then you should access that variable to pass it to the ejs as in the example (step 4) …

    What I need is to pass the array that the function gives me, to the html/ejs document as in the example.