Here's my Code and the error occur in my terminal
CodePudding user response:
Try to use async await
and change your code to:
router.get('/:id', async (req, res) => {
try {
const id = req.params;
const upload = await Upload.findById(id);
const myUsers = await User.find({});
return res.render('share', { upload, myUsers })
} catch (err) {
console.log(err)
}
})
CodePudding user response:
The problem is where you are setting res.render() in your server code more than once. Res.render combines the sending of data and closing the socket, so you can only call it once for each incoming request. You can do the async/await approach as @lpizzinidev mentions to buffer up data and make it more readable/maintainable, or chain your promises by doing a .then, execute second query inside that one, mash those two query results in one object, and then res.render(combinedData).
If you want to send data in chunks, you need to change to res.render to res.write (which you can call multiple times) before calling res.end().