I try to make 45 post request to my database, but he always just includes a few rows and throws no errors for the others. I tried some try/catch blocks and also tried axios.all, but when I push my requests array, the requests are getting executed immediately. Sometimes just 5 requests get executed and sometimes 15, but never more.
Here is my client-side code:
const insertCanteens = () => {
let requests = [];
let count = 0;
canteens.map(canteen => {
console.log("Post" + canteen.id);
requests.push(
Axios.post(`${databaseLocation}/api/insert`, {
id: canteen.id,
name: canteen.name,
city: canteen.city
}))
count++;
})
console.log("Count: " + count);
// axios.all(requests);
console.log(requests);
};
And here my server-side code:
app.post('/api/insert', (req, res) => {
const id = req.body.id;
const name = req.body.name;
const city = req.body.city;
const sql = `INSERT INTO canteens (id, name, city) VALUES (?,?,?)`;
try {
db.query(sql, [id, name, city], (err, result) => {
if (err) {
console.log(err);
}
else {
console.log("Sucessfully inserted! aaa");
// console.log(result);
}
});
} catch (e) {
console.log(e);
}
});
If I try it like this, I get no response or error and he still executes just a few requests.
const insertCanteens = () => {
let count = 0;
canteens.map(canteen => {
Axios.post(`${databaseLocation}/api/insert`, {
id: canteen.id,
name: canteen.name,
city: canteen.city
}).then( response =>
console.log(response)
).catch(error => {
console.log(error + " something went wrong!");
})
})
};
I also tried it with async and await, but same result:
const insertCanteens = () => {
let count = 0;
canteens.map(async canteen => {
await Axios.post(`${databaseLocation}/api/insert`, {
id: canteen.id,
name: canteen.name,
city: canteen.city
})
})
};
What am I doing wrong?