Mike Warren
Mike Warren

Reputation: 3866

Connecting to RDS database from node.js

I am trying to learn node.js so that I can actually get started on working on my personal project. I have been trying to follow the examples in the book "Learning node.js" (by Marc Wandschneider). I, at this point, decided to forgo practicing his example, and go straight into using his code as framework for my practice code.

In my practice code, all I am trying to do is connect to my RDS database (no, I am not using Elastic Beanstalk, btw), and output contents of one of the tables. Seems simple enough, but when I whip up the code for it (based on the book), it seems to attempt connection, but get hung up in the process. This is my code:

var pool = require('generic-pool');
var async = require('async');
var mysql = require('mysql');

var host = "<database-name>.cu8hvhstcity.us-west-2.rds.amazonaws.com",
    database = "<database-name>", 
    user = "<username>",
    password = "<someLongPassword>";

var dbClient; 

async.waterfall([
    // 1. establish connection to database
    function (callback) {
        console.log("Connecting to database " + database + "...");
        dbClient = mysql.createConnection({
            host: host,
            database: database,
            user: user,
            password: password,
            port: 3306
        });
        dbClient.connect();
    },
    // 2. select all from a table (let's go for locations)
    function (cb)
    {
        var query = "SELECT * FROM locations"
        console.log("running query \"" + query + "\"...");
        dbClient.query(query, cb);  
    },

    function (rows, fields, callback)
    {
        console.log(fields);
        for (var i = 0; i < rows.length; i++)
        {
            console.log(JSON.stringify(rows, null, '\t'));
        }
    }



], function (err, results) {
    if (err)
    {
        console.log("An error occurred...");
        console.log(err);
    }
    else
    {
        console.log("Everything successfully completed!");
    }       
    dbClient.end();
})

This is better than first attempt, when I put a database member to the argument passed to mysql.createConnection(), and it complained that database was unknown. In neither case did either "An error occurred..." nor "Everything successfully completed!" output to the window.

Is there any async stuff going on that is resulting in some kind of non-terminating infinite loop or something? How do I fix this?

The book has an associated GitHub page

NOTE:

Neither my example nor the cited GitHub code make use of that pool variable, so it can simply be commented out. All you need to do to run this yourself is to say npm install async,npm install mysql (as well as creating a dummy RDS database to point to, that contains locations table) before copying, pasting, and running this code.

EDIT:

I fixed the issue with database. I realized that the name of the database used '_', not '-'. Same issue (code hangs) still persists...

Upvotes: 0

Views: 6953

Answers (1)

Mike Warren
Mike Warren

Reputation: 3866

I did the following:

In the second function in the array, I needed two parameters, not one. I fixed thus:function(results, cb) The third function simply needed to callback(null)

Upvotes: 1

Related Questions