Nodejs service returns undefined

Guys, i’ve created a node.js service in DF using 2 packages (speakeasy and qrcode). In that service i’m trying to define some functions according to Node custom scripting service math tutorial but when the script tries to create the qrcode, i asume it takes quite time to respond because the service finish before return something. Maybe is more a node.js error than a dreamfactory error, but the code i’m using i took from a node.js script (wich uses express to expose the apis).

This is my code:

var resource, result;
const speakeasy = require('speakeasy');
const QRCode = require('qrcode');
var methods = ['GET', 'POST', 'DELETE'];
if (!methods.includes(event.request.method)) {
    throw new Error("Only HTTP GET, POST and DELETE are allowed on this service.");

// get resource, /tfa —> "", /tfa/setup —> "setup"
resource = event.resource;
switch (resource) {
    case "":
        // /tfa means return all supported resources
        result = {"resource": ["setup", "verify"]};
    case "setup":
        if(event.request.method == 'POST'){
            console.log('DEBUG: Received TFA setup request');
            const secret = speakeasy.generateSecret({
                length: 10,
                name: 'user',
                issuer: 'Portal v1.0'
            console.log('secret: ' + JSON.stringify(secret));

            var url = speakeasy.otpauthURL({
                secret: secret.base32,
                label: 'user',
                issuer: 'Portal v1.0',
                encoding: 'base32'

            QRCode.toDataURL(url, (err, dataURL) => {
                console.log('dataURL: ' + dataURL);

                result = {
                    message: 'TFA Auth needs to be verified',
                    tempSecret: secret.base32,
                    tfaURL: secret.otpauth_url
        if(event.request.method == 'GET'){
            result = {
                message: 'GET setup TFA',
                tempSecret: 'secret'
    case "verify":
        result = {"result": "verify"};
        throw new Error("Invalid or missing resource name.");
console.log('return result: ' +  JSON.stringify(result));
return result;

When i check the log, it shows this:

What i want to know what to know to return the complete result object, including the dataURL. Thanks in advance.