I am writing a simple node.js script that iterates over a set of numbers. I can see on the for loop, it's being read properly.
However, when it goes to a db query function, it's only display the +1 value for all and not the actual index loop count.
Result
Processing: 1
Processing: 2
Processing: 3
Processing: 4
Processing: 5
Processing: 6
Processing: 7
Processing: 8
Processing: 9
Processing: 10
Create New Bottle for 11
Create New Bottle for 11
Create New Bottle for 11
Create New Bottle for 11
Create New Bottle for 11
Create New Bottle for 11
Create New Bottle for 11
Create New Bottle for 11
Create New Bottle for 11
Create New Bottle for 11
Expected
Processing: 1
Processing: 2
Processing: 3
Processing: 4
Processing: 5
Processing: 6
Processing: 7
Processing: 8
Processing: 9
Processing: 10
Create New Bottle for 1
Create New Bottle for 2
Create New Bottle for 3
Create New Bottle for 4
Create New Bottle for 5
Create New Bottle for 6
Create New Bottle for 7
Create New Bottle for 8
Create New Bottle for 9
Create New Bottle for 10
Code
ProcessBottleGeneration()
async function ProcessBottleGeneration() {
try {
var start = 1
var end = 10
for (count = start; count <= end; count++) {
console.log("Processing: ", count)
GetByUniqueCode(count).then(result => {
if (result == undefined) {
console.log("Create New Bottle for ", count)
// AddBottleData(bottle).then(res => {
// console.log(res)
// }).catch(err => {
// console.log(err)
// })
}
else {
console.log("Bottle exist already for ", count)
}
// process.exit()
})
.catch(error => {
console.log("error: ", error)
return error
})
}
} catch (error) {
console.log("ProcessBottleGeneration.Error:")
console.log(error)
return error
}
}
function GetByUniqueCode(unique_code) {
var where = [
unique_code
]
let sql = `SELECT *
FROM
plants_bottles
WHERE
unique_code = ?;`
return new Promise((resolve, reject) => {
db.query(sql, [where], function (err, result) {
if (err) {
return reject(err)
} else {
return resolve(result[0])
}
});
})
}
function AddBottleData(d) {
console.log("AddBottleData:", d)
let sql = `INSERT INTO plants_bottles SET ?;`
return new Promise((resolve, reject) => {
query = db.query(sql, [d], function (err, result) {
console.log("AddBottleData.result:", result)
if (err) {
return reject(err)
} else {
return resolve(result)
}
});
})
}
What could I be missing?