-1

I am new to nodejs i try to use nested callbacks but it gives server crashes most of the time with async nature how can i change my code with async and await with out crashing my server. the error was query undefined.

exports.rpitop = function (req, resp) {
        db.executeSql("SELECT * from Kunden_settings where Benutzer='"+req.body.username+"'", function (data, err) {
            if (err) {
                resp.writeHead(200, { "Content-Type": "text/html" });
                resp.write("<html><head><title>500</title></head></html>");
            }
            else {
                var final_region;
                switch (data[0].product) {
                    case 3:
                        switch (data[0].region) {
                            case 6:final_region  = "nord_h";break;
                            case 7:final_region  = "n_ost_h";break;
                            case 8:final_region  = "ost_h";break;

                        } break;
                    case 4:
                        switch (data[0].region) {
                            case 9:final_region  = "ost_2_d";break;
                            case 10:final_region = "s_ost_d";break;
                            case 11:final_region = "mitte_d";break;

                        } break;
                    case 5:
                        switch (data[0].region) {
                            case 12:final_region = "west_e5";break;
                            case 13:final_region = "r_main_e5";break;
                            case 14:final_region = "s_west_e5";break;

                        } break;
                }

                db.executeSql("SELECT max("+ final_region +") as high, min("+ final_region +") as low from rpi_daten_neu ", function (maxmin, err) {

                    db.executeSql("SELECT * from rpi_daten_neu inner join werte_inc W where datum = (W.bezugsdatum) order by zeit desc limit 1", function (current, err) {
                    if (err) {
                        resp.writeHead(200, { "Content-Type": "text/html" });
                        resp.write("<html><head><title>500</title></head></html>");
                    }
                    else {
                        resp.writeHead(200, { "Content-Type": "x-application/json" });
                        var finalvalue = []
                            for (var i = 0; i < maxmin.length; i++) {
                                for (var i = 0; i < current.length; i++) {
                                    finalvalue.push({
                                        high: maxmin[i].high,
                                        low: maxmin[i].low,
                                        time:momenttime(current[0].zeit, 'HH-mm-ss').format("HH:mm"),
                                        value:current[0][final_region]

                                    })
                                }
                            }

                        resp.write(JSON.stringify(finalvalue));
                    }
                    resp.end();
                });
                });

            }

        });

    }; 
U rock
  • 717
  • 2
  • 13
  • 32
  • You seem to have swapped parameter order in `function (maxmin, err)` and `function (current, err)` – Bergi May 09 '17 at 16:29
  • 1
    Before using `async`/`await`, learn to use promises, especially about [promisification](http://stackoverflow.com/q/22519784/1048572) – Bergi May 09 '17 at 16:30
  • Why would you give the user a 200 error only to display a 500 error?? – dacopenhagen May 09 '17 at 16:44
  • Asking us how to use `async/await` is an incredibly broad question. It would work better for you to read about promises and then `async/await`, attempt to implement your code with those technologies and then come back here with specific implementation questions when/if you get stuck. You're essentially asking us to teach you multiple levels of asynchronous response handling when there are thousands of tutorials already on the web that you need to start with first. – jfriend00 May 09 '17 at 17:33

1 Answers1

0

For using async await you need start your node app with flag --harmony-async-await:

node --harmony-async-await server.js

or

node --harmony server.js

Next you need rewrite all your callback functions to functions that return promise. You can do it by yourself but also you can use for example https://www.npmjs.com/package/es6-promisify package.

//all functions with await prefix in async function must return promise
const func1 = ()=>{
  return new Promise((resolve, reject)=>{
     ...
  })
};

//all functions with await prefix in async function must return promise
const func2 = ()=>{
  return Promise.resolve(/*your result*/);
}


//async await function example
const asyncFunc = async function(){
    let a = await func1();
    let b = await func2();
    ...
}
Sergaros
  • 821
  • 1
  • 5
  • 14