I have this post where I perform an insert and I need to get the id to be able to use it in another insert.
router.post('/save_card', verifyToken, (req, res) => {
const page = req.body.page;
const container_card = req.body.container_card;
mysqlConnection.query('INSERT INTO pages (name_page) VALUES (?)', [page],
(err, results, fields) => {
if (err) throw err;
container_card.forEach(card => {
mysqlConnection.query('INSERT INTO cards (id_page, container_card) VALUES (?,?)', [results.insertId, card],
(err, rows, fields) => {
if (err) throw err;
res.json({
message: 'Card created successfully',
});
});
});
});
});
it works but it gives me an error
C:\Users\lcardemi\Desktop\PROYECTO CARD 3.0\backend\node_modules\mysql\lib\protocol\Parser.js:437 throw err; // Rethrow non-MySQL errors ^ Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client