0

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

9
  • Try to use this code:link.connect(async function(err) { if(err) throw err; await processArguments(); }); Commented Dec 8, 2018 at 18:11
  • it doesn't work Commented Dec 8, 2018 at 18:16
  • check this filter in your query WHERE kunde.K_ID=item.K_ID Commented Dec 8, 2018 at 18:20
  • I guess you have hard coded it with a string instead of passing a value.Use object literals if you actually want to do this Commented Dec 8, 2018 at 18:20
  • I think there is something wrong with your sql query itself. Try to run it wither from mysql workbench or from terminal Commented Dec 8, 2018 at 18:22

1 Answer 1

1

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(',')})`;
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, it works. I forgot async before function at connect

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.