0

I am trying to develop a simple Alexa Skill that connects to a postgresql database using node.js and returns the results.

I can connect to the database using node.js from my local machine but cannot when uploading to a lambda function on aws.

I am still very new and learning. I have done a good amount of research before posting but no luck.

 'query_one': function () {

        const { Pool, Client } = require('pg')

        const pool = new Pool({
            user: 'username',
            host: 'host information',
            database: 'database name',
            password: 'password',
            port: 5432,
        })

        pool.query('SELECT * FROM responses', (err, res) => {
            console.log(res.rows[1].answer)
            pool.end()
        })

        this.emit(':tellWithCard', 'test', this.t('SKILL_NAME'), 'test');

    }

full code including

'use strict';


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

const APP_ID = 'app id is hidden from here';  // TODO replace with your app ID (OPTIONAL).


const languageStrings = {
    'en': {
        translation: {
            FACTS: [
                'SELECT answer FROM responses WHERE query_id=1',
                'SELECT answer FROM responses WHERE query_id=2',
            ],
            SKILL_NAME: 'Space Facts',
            GET_FACT_MESSAGE: "Here's your fact: ",
            HELP_MESSAGE: 'You can ask me questions about the health of the system, or, you can say exit... What can I help you with?',
            HELP_REPROMPT: 'What can I help you with?',
            STOP_MESSAGE: 'Goodbye!',
            WELCOME: 'Welcome to the health check monitor',
        },
    },
};


const handlers = {

    'LaunchRequest': function () {
        const welcome = this.t('WELCOME');
        this.emit(':tellWithCard', welcome, welcome, welcome);
    },
    'query_one': function () {

        const { Pool, Client } = require('pg')

        const pool = new Pool({
            user: 'username',
            host: 'host information',
            database: 'database name',
            password: 'password',
            port: 5432,
        })

        pool.query('SELECT * FROM responses', (err, res) => {
            console.log(res.rows[1].answer)
            pool.end()
        })

        this.emit(':tellWithCard', 'test', this.t('SKILL_NAME'), 'test');

    },
    'query_two': function () {
        const factArr = this.t('FACTS');
        const randomFact = factArr[1];
        const speechOutput = this.t('GET_FACT_MESSAGE');
        this.emit(':tellWithCard', speechOutput, this.t('SKILL_NAME'), randomFact);
    },
    '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'));
    },
};

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();
};
James Z
  • 12,209
  • 10
  • 24
  • 44
W.Sar
  • 126
  • 1
  • 6

1 Answers1

0

Well first off all of your emits will fail, here is the proper format. this.emit(':tellWithCard', speechOutput, cardTitle, cardContent, imageObj); For information in connecting to your database see this post How to make connection to Postgres via Node.js

Enjoy, glad to help!

Hunter
  • 388
  • 4
  • 11
  • Hello Hunter, thanks for you response. The emits are currently working as expected. However the connection to the database is working fine on my local however same code is not working in the lambda function. I am trying to get the query results to print in the log for now. – W.Sar Oct 28 '17 at 00:48
  • Hmm I dont know lambda, I use nodejs all around. And hit a custom https endpoint and save any variables to json objects. Then read them as json objects. what about https://blog.risingstack.com/getting-started-with-aws-lambda-and-node-js/ ? You could also just spend $5 a month and go to https://www.digitalocean.com and get a droplet and then you have full control. And your code work perfectly fine, since you already know it works from nodejs. Eliminating the amazon middleman – Hunter Oct 28 '17 at 00:51