[ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

后端 未结 3 977
广开言路
广开言路 2021-02-19 18:28

I\'m working with PostgreSQL and NodeJS with its \"PG Module\". CRUD works but sometimes doesn\'t update automatically the views when i save or delete some item. this is my cod

3条回答
  •  别那么骄傲
    2021-02-19 18:52

    You need to embed your response in the callback to the query. Since the call is asynchronous, sending the response earlier will terminate the call stack never waiting for the webapi(Behaviour may vary).

    controller.delete = (req, res) => {
        pool.query('DELETE FROM RECIPES WHERE ID = $1', [req.params.id],(err, result) 
         => {
             // error handling can be done accordingly
            return res.redirect('/');
        })
    
    }
    

提交回复
热议问题