Запрос AWS RDS MySQL из функции Alexa AWS Lambda Node.js

Я подозреваю, что обратный вызов в моем коде не вызывается или к тому времени, когда он вызывается, функция уже возвращает значение по умолчанию вызывающей стороне. Я видел, как McElroy использовал функцию context.succeed(), чтобы это исправить. Я не понимаю, как применить это к моей ситуации, когда вызов SQL происходит из функции, вызываемой обработчиком...

/* eslint-disable  func-names */
/* eslint quote-props: ["error", "consistent"]*/
/**
 * This sample demonstrates a simple skill built with the Amazon Alexa Skills
 * nodejs skill development kit.
 * This sample supports multiple lauguages. (en-US, en-GB, de-DE).
 * The Intent Schema, Custom Slots and Sample Utterances for this skill, as well
 * as testing instructions are located at https://github.com/alexa/skill-sample-nodejs-fact
 **/

'use strict';

const Alexa = require('alexa-sdk');

const APP_ID = undefined;  // TODO replace with your app ID (OPTIONAL).

const states = {
    START: "_START",
    NEED: "_NEED"
};

const languageStrings = {
    'en': {
        translation: {
        },
    },
};

function fnGetPatientID(strRoom) {

    const libSql = require('mysql');

    var strResults = 'in Room ' + strRoom;

    var objConnection = libSql.createConnection({
        host: 'mydb.blahblah.us-east-1.rds.amazonaws.com',
        port: '3306',
        user: 'somenotrealusername',
        password: 'somenotrealpassword'
    });

    console.log(`In function called by Intent Request`);
    console.log('Then run MySQL code:');

    objConnection.connect(function(err) {
        console.log('Inside connection.connect() callback');
        if (!err) {
            console.log("Database is connected ... ");

            objConnection.query('SELECT strPatientFirstName, strPatientLastName FROM tblRoomAssignment WHERE strRoom = "' + strRoom + '"', function(err, rows, fields) {
                console.log("Inside connection.query() callback")
                if (!err) {

                    strResults = rows[0].strPatientFName.value;
                    console.log("Query Successful! Ending Connection.");
                    objConnection.end();

                } else {
                    console.log("Query error!");
                }
            });
        } else {
            console.log("Error connecting database ..." + err.message);
        }
    });
    context.succeed();
    return strResults;
}

function fnGetMedicationList(objectNeeded, timePeriod) {
    var strList = "";
    var objectItem = objectNeeded.toLowerCase();
    if ((objectItem == "medicines") ||(objectItem == "pills") || (objectItem == "meds") || (objectItem == "medications")) {
        strList = "1. Ibuprofen, 400mg, with breakfast.\n2. Metoprolol, 20mg, after lunch.";
    } else if ((objectItem == "surgeries") || (objectItem == "tests") || (objectItem == "procedures")) {
        strList = "1. CT Scan at 9:00 AM.\n2. Quadruple Bipass at noon.";
    }
    return strList;
}

function fnOutput(objThis, strOutputVoice, strCardTitle, strOutputVisual) {
    var strCardContent = strOutputVisual;
    var imgObj = {
        smallImageUrl: 'https://s3.amazonaws.com/gxassistance/logo-small.png',
        largeImageUrl: 'https://s3.amazonaws.com/gxassistance/logo-small.png'
    }
    
    objThis.emit(':tellWithCard', strOutputVoice, strCardTitle, strCardContent, imgObj);
}

function fnInput(objThis, strOutputVoice, strOutputVoiceRepeat, strCardTitle, strOutputVisual) {
    var strCardContent = strOutputVisual;
    var imgObj = {
        smallImageUrl: 'https://s3.amazonaws.com/gxassistance/logo-small.png',
        largeImageUrl: 'https://s3.amazonaws.com/gxassistance/logo-small.png'
    }
    objThis.emit(':askWithCard', strOutputVoice, strOutputVoiceRepeat, strCardTitle, strCardContent, imgObj);
}

const handlers = {
    'LaunchRequest': function () {
        // this.emit(':askWithCard', this.t('START'), this.t('START_REPEAT'), this.t('SKILL_NAME'), this.t('START_DISPLAY'));
        fnInput(this, this.t('START'), this.t('START_REPEAT'), this.t('SKILL_NAME'), this.t('START_DISPLAY'));
    },
    'AssistanceIntent': function () {
        fnInput(this, this.t('START'), this.t('START_REPEAT'), this.t('SKILL_NAME'), this.t('START_DISPLAY'));
    },
    'MedicationListIntent': function () {

        // Default response
        var adviceOutputVoice = this.t('DEFAULT');
        var adviceOutputDisplay = this.t('DEFAULT');

        // If the need and time period are identified
        var objectSlot = this.event.request.intent.slots.ObjectNeeded.value;
        var timePeriodSlot = this.event.request.intent.slots.TimePeriod.value;
        if ((typeof(objectSlot) != "undefined") && (typeof(timePeriodSlot) != "undefined")) {
            adviceOutputVoice = this.t('LIST1') + objectSlot + ' for patient [' + fnGetPatientID('1001') + '] ' + this.t('LIST2') + timePeriodSlot + this.t('LIST3') + fnGetMedicationList(objectSlot, timePeriodSlot);
            objectSlot = objectSlot.charAt(0).toUpperCase() + objectSlot.slice(1);
            adviceOutputDisplay = this.t('LIST_DISPLAY1') + objectSlot + ' for patient ' + fnGetPatientID('1001') + ' ' + this.t('LIST_DISPLAY2') + timePeriodSlot + this.t('LIST_DISPLAY3') + fnGetMedicationList(objectSlot, timePeriodSlot);
        }
        var strCardTitle = this.t('SKILL_NAME');
        fnOutput(this, adviceOutputVoice, strCardTitle, adviceOutputDisplay);
    },
    'AMAZON.HelpIntent': function () {
        const speechOutput = this.t('HELP_MESSAGE');
        const reprompt = this.t('HELP_MESSAGE');
        this.emit(':ask', speechOutput, reprompt);
    },
    'AMAZON.CancelIntent': function () {
        this.emit(':tell', this.t('STOP_MESSAGE'));
    },
    'AMAZON.StopIntent': function () {
        this.emit(':tell', this.t('STOP_MESSAGE'));
    },
    'Unhandled': function () {
        this.emitWithState('NurseCallIntent');
    },
};

exports.handler = function (event, context) {
    const alexa = Alexa.handler(event, context);
    alexa.APP_ID = APP_ID;
    // To enable string internationalization (i18n) features, set a resources object.
    alexa.resources = languageStrings;
    alexa.registerHandlers(handlers);
    alexa.execute();
};

0 ответов

Другие вопросы по тегам