PubNub FunctionsDebugging with console.log()

In this exercise, we'll learn a bit more about using console.log() to display variable values for troubleshooting purposes.

As we continue onward with more complicated logic, it's important to keep in mind that the console.log() method is a great tool for debugging – console.log() outputs the stringified value of the object you pass into it.

Building on our Hello World example, let's optimize our logging a bit… we'll now log out:

  • The original message body.
  • The channel the message arrived on.
  • The client IP address.
  1. export default (request) => {
        console.log("The message:", request.message, "was published on", request.channels[0], "via", request.meta.clientip);
        request.message.hello = "world!"; // augment hello = world
        delete request.message.foo; // delete the foo attribute
        return request.ok(); // Return a promise when you're done
    };
  2. Click Save.
  3. Click Restart Module. Once running, let's once again test this functionality using the built-in test console.
  4. {
    	"foo":"bar"
    }
  5. Click Publish.

As seen from the output, we've logged out the needed attributes via console.log().

Console.log Ouput

The console.log() call will prove to be a very useful troubleshooting and debugging tool… use it liberally!

 

The limit on console.log of 250. This is to protect a user from generating too many debugging publishes in a production Module.

During debugging a simple restart of the block will reset the limit.

 
PubNub Functions provides a rich set of tools, and this documentation does not cover all of the potential situations you may encounter. If you need help with a situation not covered by the documentation, please contact PubNub Support.