alexa-js / alexa-app

A framework for Alexa (Amazon Echo) apps using Node.js
https://www.youtube.com/watch?v=pzM4jv7k7Rg
MIT License
1.03k stars 212 forks source link

Mysql Promise #334

Closed kreak445 closed 6 years ago

kreak445 commented 6 years ago

Hey, i try to use mysql, but always the mysql do not resolve before the skript is done. I try many things, like .pre function or a Database class with promise. The doc says that i could use promise. Here is the code, which 'should' work:

function getLastRecord(query)
{
    return new Promise(function(resolve, reject) {

        connection.query(query,function (err, result, fields) {
            if (err) {
                return reject(err);
            }
            resolve(result);
        });
    });
}

var app = new alexa.app('icinga');

app.launch(function(req, res) {
  res.say("Hi").reprompt(reprompt).shouldEndSession(false);
});

app.intent('NameStatus', {
    "slots": { "TEST1": "TEST" },
    "utterances": ["wie ist der Status von {TEST1}"]
    }, function(req, res) {
    console.log("1");
    if(req.slot('TEST1')){
        var test1= host_dict[req.slot('TEST1')]
        var query = 'select XXXXXXX WHERE test = "' + test1+ '" ORDER BY XXXX LIMIT 1;';        
        console.log("2");
        getLastRecord(query)
            .then(function(out) {
            console.log("3");
            if(out[0].state == "0"){
                speechOutput = out[0].name1 + 'is there';
            }
            else{
                speechOutput = out[0].name1 + ' ist not there';
            }

            res.say(speechOutput).shouldEndSession(true);
            }).catch((err) => setImmediate(() => { throw err; }));
    }
    else{
    speechOutput = 'Input Error';
    res.say(speechOutput).shouldEndSession(true);
    }   
    console.log("4");
});

Now i want see on my console 1 2 3 4

But it is 1 2 4 3

Could someone help me?

kobim commented 6 years ago

Your script is just fine, but your numbering is wrong. "4" is printed in the end of the function, which in most cases will be before the Promise is resolved.

If you are using node 8+ with async/await, you can try a more streamlined approach (then your 1,2,3,4 will be printed accordingly):

app.intent('NameStatus', {
    "slots": {
        "TEST1": "TEST"
    },
    "utterances": ["wie ist der Status von {TEST1}"]
}, async function(req, res) {
    console.log("1");
    if (req.slot('TEST1')) {
        var test1 = host_dict[req.slot('TEST1')]
        var query = 'select XXXXXXX WHERE test = "' + test1 + '" ORDER BY XXXX LIMIT 1;';
        console.log("2");
        try {
            const out = await getLastQuery(query);
            console.log("3");
            if (out[0].state == "0") {
                speechOutput = out[0].name1 + 'is there';
            } else {
                speechOutput = out[0].name1 + ' ist not there';
            }
            res.say(speechOutput).shouldEndSession(true);
        } catch (err) {
            console.error('Query error:', err); // Don't just re-throw the error
            res.say('Error, try again');
        }
    } else {
        speechOutput = 'Input Error';
        res.say(speechOutput).shouldEndSession(true);
    }
    console.log("4");
});

Except for the script output, is the intent being answered correctly when you test it?

kreak445 commented 6 years ago

Hey kobim, thanks, now it works fine with your code. with my code it also works, but the sql output was t -1 so after booting the server i ask for "cookies" and i got an empty answer <speak></speak>

and the second time i ask for "milk" and i got <speak>cookies</speak>

so the promise dosnt work right and even with your code, which is working, i dont know why my code dosnt work :-(

But Im happy for your greate code