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

1 Like

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

1 Like

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.

Hey there!

As this topic is related to Actions and Rules & Hooks are being deprecated soon in favor of Actions, I’m excited to let you know about our next Ask me Anything session in the Forum on Thursday, January 18 with the Rules, Hooks and Actions team on Rules & Hooks and why Actions matter! Submit your questions in the thread above and our esteemed product experts will provide written answers on January 18. Find out more about Rules & Hooks and why Actions matter! Can’t wait to see you there!

Learn more here!