How do I add multiple records at once in MySQL by passing an array of, for example, products?
HTTP-POST request, where products is an array with an x number of products. FRONT-END
addProduct(products: any): Observable<any> {
return this.http.post<any>(this.productURL, products, httpOptions)
}
BACK-END
// Add Products
app.post('/products', function (req, res) {
let products = req.body;
console.log(products)
if (!products) {
console.log('Deu erro na bagaça')
return res.status(400).send({ error: true, message: 'Please provide products' });
}
mydb.query('INSERT INTO products SET ${products} ', function (error, results, fields) {
if (error) throw error;
// console.log(results)
return res.send(results);
});
});
console.log () in the BACK-END of the variable sent to the back