I am trying to insert the query in nodeJs where the transactionCode
will automatically generate. but in my case when I insert in all the 5 insert query inserting with the same transactionCode
as ABC123
but it should be inserted with ABC123, ABC124, ABC125, ABC126, AB127
an even I am calling this generate function in the loop then why it not working and generating one by one and inserts.
please help how me fix this issue and make my code correct.
for (let i = 0; i < 5; i++) {
var stmt, transactionCode;
stmt = await con.query("SELECT `token` FROM `table1` ORDER BY `ID` DESC LIMIT 1", {
type: con.QueryTypes.SELECT,
});
// this function will return auto number with +1 to with previous number, suppose my previous number is ABC123 then my next number will ABC124
if (stmt.length > 0) {
stmt.map(async (item) => {
transactionCode = item.token;
let strings = transactionCode.replace(/[0-9]/g, "");
let digits = (parseInt(transactionCode.replace(/[^0-9]/g, "")) + 1).toString();
if (digits.length < 2) digits = ("000" + digits).substr(-3);
transactionCode = strings + digits;
});
} else {
transactionCode = "ABC123";
}
let stmt1;
stmt1 = await con.query("INSERT INTO `table2` (`col1`,`col2`) VALUES(:token, :name)", {
replacements: {
token: transactionCode,
name: req.body.name
},
type: con.QueryTypes.INSERT,
transaction: transaction,
});
}
transaction.commit();
return res.json({ code: 200, message: "Generated", status: "success" });