Reputation: 543
could someone help me with my problem? I'm new to JavaScript (Node.js) and try to perform MySQL queries but this outputs nothing. I don't know how to handle this.
async function processArguments() {
var result_customer = [];
for(let i = 2; i < process.argv.length; ++i) {
let sql = "SELECT Mandantennummer, Firmenname, VornameAnsp, NachnameAnsp, Telefonnummer, Strasse, PLZ"
"FROM kunde, item"
"WHERE kunde.K_ID=item.K_ID AND I_ID="+ process.argv[i];
await link.query(sql, function(err_customer, customer, fields_customer) {
if(err_customer) throw err_customer;
result_customer.push(customer);
});
}
for(let i = 0; i < result_customer.length; ++i) {
console.log(result_customer[i].Firmenname);
}
link.end();
}
link.connect(function(err) {
if(err) throw err;
processArguments();
});
This outputs nothing
Thanks for help
Upvotes: 0
Views: 58
Reputation: 12542
You are using callback
functions as Promise
which will not work. You need to promisify the query.
async function processArguments() {
var result_customer = [];
for (let i = 2; i < process.argv.length; ++i) {
let sql = "SELECT Mandantennummer, Firmenname, VornameAnsp, NachnameAnsp, Telefonnummer, Strasse, PLZ"
"FROM kunde, item"
"WHERE kunde.K_ID=item.K_ID AND I_ID=" + process.argv[i];
const customer = await new Promise((resolve, rej) => {
link.query(sql, function (err_customer, customer, fields_customer) {
if (err_customer) rej(err_customer);
resolve(customer);
});
});
result_customer.push(customer);
}
link.end();
}
link.connect(function (err) {
if (err) throw err;
await processArguments();
});
If you want to await
, it needs to be a Promise
or an async
function.
Edit: You can concat the process.argv[i] and use it along with IN
in that query.
let sql = `SELECT Mandantennummer, Firmenname, VornameAnsp, NachnameAnsp, Telefonnummer, Strasse, PLZ
FROM kunde, item
WHERE kunde.K_ID=item.K_ID AND I_ID IN (${process.argv.slice(2).join(',')})`;
Upvotes: 1