0

I am trying to build an email for a user in nodejs:

function makeOptions(user, updatedresults) {
    var mailOptions = {
        'from': ['contact@mywebsite.com', 'Site Name'],
        'to': {
            'mymail@mail.com': 'Admin'
        },
        'subject': "New results for your bookmark " + bookmark,
        'html': '<p>Bonjour <b> ' + user.nomprenom + '!</b><br>' +
            '<b>New results :<b>Updated results ' + updatedresults.length + ' :</b><ul>' + rapportENEWS(updatedresults, 'en') + '</ul>'
    };
    console.log(mailOptions.html);
}

function rapportENEWS(data, lang) {
    var htmlbookmark = '';
    var sqlbookmarks = "select id, title, pertinence where id in (" + data + ") ORDER BY pertinence";
    connection.query(sqlbookmarks, function(err, res) {
        var counter = 0;
        for (var i = 0; i < res.length; i++) {
           if (i == 0) {
                htmlbookmark += '<h2 style="font-size:14px; text-decoration:underline; color:#0A4369">' + res[i].pertinence + '</h2>';
            } else if (i > 0 && res[i - 1].pertinence != res[i].pertinence) {
                htmlbookmark += '<h2 style="font-size:14px; text-decoration:underline; color:#0A4369">' + res[i].pertinence + '</h2>';
                htmlbookmark += '<table style="width:100%;font-size:12px;">Updated articles';
                htmlbookmark += "\n" + '</tr><tr><td style="padding-bottom:8px;border-bottom:3px solid #bfbfbf;" colspan="2"><strong><span style="color: #993300;">' + res[i].title + ' ' + getMagic(res[i].id) + '</span></strong></td></tr></table>';
            }
            if (counter == res.length) {
                //HERE THE RETURN
                console.log(htmlbookmark);
                return htmlbookmark;

            } else {
                counter++;
            }
        }
    });
}

When I check my console, I get undefined (console.log of mailOptions.html). I made some console.log in the called function, and it show result but it does not return it.

Note: The function getMagic(res[i].id) return a number and works fine.

AmenzO
  • 409
  • 7
  • 19
  • 1
    Why would it return it, there's no `return` statement in that function of any kind ? – adeneo Oct 17 '16 at 13:07
  • Also, `rapportENEWS` is asynchronous, and you try to use it synchronously in `makeOptions` – DrakaSAN Oct 17 '16 at 13:08
  • Adeno : of course at the end of code : return htmlbookmark; Thank you DrakaSan for explain, could you tell me how to use it asynchronously ? thank you – AmenzO Oct 17 '16 at 13:23

0 Answers0