0

I want to call a function on the returned data from this database query, as in the snippet below.

function connectDB(query, data, callback) {
    pg.connect(process.env.DATABASE_URL, function (err, client) {
        if (err) {
            console.log(err);
        } else {
                client
                    .query(query, data, function (err, result) {
                        callback(result.rows);                            
                    });
        }
     }
}

function foo(data) {
  connectDB("SELECT * FROM mytable WHERE id=$1", someID, callbackfn(data));
}

I don't know how to write callbackfn so that I can use both data from the original function and rows from the db result.

function callbackfn(data) {
  return function(rows) {
   // Do something with rows and data
  }
}
amo
  • 4,082
  • 5
  • 28
  • 42
  • 1
    You've defined `callback` as a parameter to `connectDB`, so unless you pass the function named `callback` to `connectDB`, it won't be calling that function. You also have a strange set of nested functions there; it's not entirely clear what you're trying to do. – Heretic Monkey May 22 '17 at 17:47
  • I clarified things a bit. Ignore the nested functions (I deleted them). – amo May 22 '17 at 19:04
  • There's no particular reason, from this code alone, why this wouldn't work. I would suggest putting `console.log('after query')` or something like that before the call to `callback` to make sure the code is getting that far. Or just use a debugger and put a breakpoint there. – Heretic Monkey May 22 '17 at 19:08
  • I edited my question to better match my actual code... although now it is actually working. All I did was remove the inner function (). – amo May 22 '17 at 19:20

3 Answers3

0

You don't accept any params with the callback function. How about:

function callback(rows) {
  /* not sure what your intention was here? nested functions? */
  return function () {
    return function () {
        // Do something with rows
    }
  }
}

And to be clear, can you post where you call the connectDB function? As callback is name of the variable after it is passed to connectDB.

Travis White
  • 1,977
  • 1
  • 11
  • 19
  • I edited the question to clarify it. Ignore the nested functions, I was trying different things and at one point thought they were necessary. – amo May 22 '17 at 19:05
0

Why are you defining callback function? Please elaborate on this. Also, try calling connectDB like this:

 connectDB(query, data, function(rows) {
       // Do something with rows
 });

Read this answer on callbacks.

Mini Bhati
  • 343
  • 5
  • 20
0

You have an incredible level of nested functions. Moreover, you are defining callback as parameter, but it won't take the callback function you defined, if you are not calling the base function passing again "callback" as argument. If you want to pass always the same callback, it doesn't make sense that you provide it as argument, you can directly call "callback" inside your "connectDB" function. Moreover your callback returns a function, so it should be called again using again (). Then your main callback function needs to accept rows as parameter from outside. I kept your code using the callback parameter because I "hope" that the same name was just an example to explain that you want to provide every time a function in order to manipulate as you want the rows. You code should look like this:

function connectDB(query, data, callback) {
    pg.connect(process.env.DATABASE_URL, function (err, client) {
        if (err) {
            console.log(err);
        } else {
                client
                    .query(query, data, function (err, result) {
                        // this returns a function so it needs to be called again
                        callback(result.rows)();                            
                    });
        }
     }
}

// third inner function was useless
function callback(rows) {
  return function () {
    // Do something with rows
  }
}

// the third parameter HAS TO BE callback, otherwise you will not pass the function you defined. 
connectDB(query, data, callback);
quirimmo
  • 9,800
  • 3
  • 30
  • 45