Log from within a callback in an Action

I’m using the mysql Node module from within an Action that runs in the Post Login flow to insert a user into my own database if necessary. I’m only storing metadata such as name and email, rather than passwords, thus this flow rather than integrating my DB as a custom DB. I’ve gotten it to work, but I ran into some difficulty where log statements weren’t doing anything from within callbacks. For instance, this code:

  console.log("Pinging DB");
  db.ping(function (err) {
    if (err) throw err;
    console.log('Server responded to ping');
  });
  console.log("Next step...");

Would result in “Pinging DB” and “Next step…” getting logged to the test output, but “Server responded to ping” would never appear. Is there a workaround for this, or some other way I should be logging from within callbacks? Just to be clear – the MySQL calls are working, so I know the callback code is getting executed, it’s just the log statements that are ineffective.

Hi @iancroptix

At a guess, your db.ping statement is causing an error, which gets thrown.
Put a console.log right before the if, and see if something shows up

John

No luck, unfortunately. Changed it to:

  console.log("Pinging DB");
  db.ping(function (err) {
    if (err) {
      console.log("Error pinging DB:", err);
      throw err;
    }
    console.log("Server responded to ping");
  });

…and still not seeing anything from the callback.

If an error is thrown, however, shouldn’t it show up in the “Errors” section after testing the Action?

Hi @iancroptix

You need a console log right before the “if”, like:

db.ping(function (err) {
console.log("Here with err: ", err);
if (err) {

John

I’m not sure how that changes anything, since the console log happens before the throw, and another log happens if there’s no error. Regardless, I added in the log right before the “if” as you indicated, and I still see no output from that statement or any other inside a callback.