V
V
Vladimir2019-11-29 07:18:54
JavaScript
Vladimir, 2019-11-29 07:18:54

How to get the response of a get request into a promise on the client?

Good morning. Can you please tell me how can I get my res.send on a get request on the client in a similar format:

(async function() {

  const response = await fetch(`${window.location.pathname}`, {method:'POST'})
  const data = response.json()

  console.log(data)

}())

app.get('/tender/:id', (req,res) => {

  res.sendFile(`${static}/www/tender.html`)

  const tender = new Promise(async(resolve,reject) => {
    await pool.query(`select * from tenders where id = '${req.params.id}'`, (error,results,fields) => {
      resolve(results.map(r => r))
    })
  }).then(r =>  {
    res.send(r)
  })

})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2019-11-29
@HistoryART

you do not have a code, but a set of letters. Subjects that you don't understand:
1. asynchronous functions, promises, apparently, and not really callbacks.
2. Express, basic understanding of http requests.
3. SQL injection.
On your question:
1. sendFile closes a flow .
2. the client code makes a post request for you, the back at least expects a get
. In general, the example in the question is a wild signal of a lack of your knowledge, a recommendation to roll back to studying the base of the language and documentation at least to express.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question