REMINDER
Sending html mail from API
-
I've tried reproducing code that works fine in one api to another(/new) api. I've checked and double checked, but I'm most likely overlooking something. From what I've concluded from my limited knowledge on debugging, is that request(urlTemplate) does nothing. And yes, the package 'request' is selected.
var urlTemplate = "https://api.appdrag.com/NewsletterViewer.aspx?newsletterID=2943&appID=webconnect-374b23"; // Replace with your own HTML template //console.log(urlTemplate); request(urlTemplate, { encoding: null }, function(error, response, body) { console.log(response); if (!error && response.statusCode == 200) { if (response.headers['content-encoding'] == 'gzip') { // If template is located on AppDrag, file is GZIP by default zlib.gunzip(body, function(err, dezipped) { emailContent = dezipped.toString(); processTemplate(); }); } else { emailContent = body.toString(); processTemplate(); } } else { callback(null, "File not found"); } });
Also, I noticed that the package 'request' is deprecated. Is this something I should worry about right now?
-
Hi,
We would need the whole code to help you debug it. Is process template in your function?
Request is deprecated yes but for the use you have of it in this case (loading a remote url) you don't have to worry at all.
-
@Wassim I sent you the code by mail ...
-
Ok,
Your issue is due to the use of ASYNC / AWAIT.Request is not promise based so you can't await it (and you don't do it at the moment, but your function is killed before it's called as it is async).
You can either
switch to a non async function globally and switch the rest of your code
or
follow this solution : https://stackoverflow.com/questions/41412512/node-js-promise-request-return -
@Wassim thanks for taking your time to help met out! I'm still learning JavaScript and the promise/async was still om my todo list. Now I think it's time to dive into this. I already collected some reading material, which might be interesting to others as well ...
https://www.w3schools.com/js/js_promise.asp
https://www.freecodecamp.org/news/javascript-from-callbacks-to-async-await-1cc090ddad99/
https://medium.com/better-programming/callbacks-vs-promises-in-javascript-1f074e93a3b5
https://medium.com/better-programming/should-i-use-promises-or-async-await-126ab5c98789
-
@Wassim said in Sending html mail from API:
https://stackoverflow.com/questions/41412512/node-js-promise-request-return
@Dick-Honing I've used the package 'request-promise-native' which allows you to await a request.
I recommend checking it out, it's been working great for me:
https://www.npmjs.com/package/request-promise-native -
@Daniel-Mulroy thanks for the tip!