0

I am trying to create a log of sent mails including some other details and need to access the info.response object from the mail transporter in node, I have tried a number of things including trying to expose the info.response as a global variable but I cannot seem to access it. I want to send a mail using the transporter then log some entry details and the info.response code in the same line of the log.txt file. The resInfo is not accessible outside of the transporter and the array clientReminderCandidates[x][1] isn't accessible from within the transporter.

index.js

            gm.clientReminders();
            // clientReminderCandidates made available

            if (clientReminderCandidates.length != 0) {
                for (x = 0; x < clientReminderCandidates.length; x++) {

                    const clientReminders = require('./sendClientReminders.js')
                    clientReminders.sendRemindClient();

                    const updateReminders = require('./updateClientReminderSent.js')
                    updateReminders.updateClientReminder();

                }
            }
            else { console.log("No client reminders to update") }

sendClientReminders.js

            module.exports = {
                sendRemindClient: function () {
                    var fs = require('fs');
                    fs.appendFile('logs/logs.txt', 'LOG Reminder sent to ' + clientReminderCandidates[x][10] + ' ' + resInfo + ' \r\n', function (err) { // cannot access resInfo outside transporter
                        if (err) throw err;
                        console.log('Reminder Sent and Logged!');
                    });

                    transporter.sendMail(mailOptions, function (error, info) {
                        resInfo = info; // I tried to reassign the `info` var from the transport function above

                        if (error) {
                            console.log(resInfo); // but this does not pull in the `info` var
                            console.log(resInfo);
                            console.log(info.response); // I need to use this in the top function @ resInfo.
                            transporter.close();
                        } else {
                            console.log(error);
                            console.log(clientReminderCandidates); // array is available
                            console.log(clientReminderCandidates[x][1]); // cannot access these objects undefined
                            console.log('Reminder Sent and Logged!');

                            transporter.close();
                        }
                    });
                }
            }

How can I access the info.response in order to use it in the fs.appendfile function to build my log entry including the response code?

ZADorkMan
  • 301
  • 4
  • 19
  • You haven't shown how and where you try to access `resInfo`, but likely you are doing it before it got assigned in the asynchronous callback. – Bergi Nov 23 '19 at 20:05
  • My apologies, I am trying to learn more about sync and async functions however I am trying to access `info` in `transporter.sendMail(mailOptions, function (error, info) {` from the function above it. The `resInfo = info;` was part of my attempt to assign it from the above function. I will edit for clarity. – ZADorkMan Nov 23 '19 at 20:47
  • Ah, I didn't see that, sorry. But no, you can never access things *before* they are assigned, so even in synchronous code the call above couldn't get the `resolveInfo` that was created below. You will need to move the `fs.appendFile(…)` call inside the `transporter.sendMail` callback, so that it runs once the mail is sent, not before the mail sending even is started. – Bergi Nov 23 '19 at 20:51
  • Thanks for your reply, indeed that was what I thought too but if I move it to inside then I get `undefined` for clientReminderCandidates[x][1] but the array is returnable when using `clientReminderCandidates` – ZADorkMan Nov 23 '19 at 20:58
  • Where do `clientReminderCandidates`, `x` and `mailOptions` even come from? It looks like you haven't posted your actual code. – Bergi Nov 23 '19 at 21:00
  • My guess would be that you're having a [closure in a loop issue](https://stackoverflow.com/q/750486/1048572) – Bergi Nov 23 '19 at 21:01
  • This is a module export to my main index.js, where I call the module from. – ZADorkMan Nov 23 '19 at 21:02
  • clientReminderCandidates is available for use when the module is loaded, it is created using another module using a map function. Whats strange for me that is if I use `clientReminderCandidates` I can get the entire array to log however as soon as I use `clientReminderCandidates[x][1]` I get `undefined`. I don't understand because I use the `clientReminderCandidates[x][1]` in the mailer options above successfully (removed from this for readability) – ZADorkMan Nov 23 '19 at 21:07
  • I think I must be missing something with passing variables between functions. Thank you – ZADorkMan Nov 23 '19 at 21:07
  • Indeed, you seem to be saying that they are global variables? At least `x` definitely should be passed as a parameter from where you call `sendRemindClient`. – Bergi Nov 23 '19 at 21:10
  • Yes x is defined as this is all wrapped in a for loop. – ZADorkMan Nov 23 '19 at 21:10
  • What "all this"? The `module.exports =` assignment? Don't do that, it won't work. Put only the `sendRemindCall(…)` in a loop, and pass `x` as an argument. – Bergi Nov 23 '19 at 21:23
  • Thank you, I have added more context to my code for reference. I do call ` clientReminders.sendRemindClient();` inside a for :) – ZADorkMan Nov 23 '19 at 21:38
  • Yes, it's just as I said. `x` is a global variable - don't do that. Pass it as a parameter, or even better, pass the whole `clientReminderCandidates[x]`. – Bergi Nov 24 '19 at 08:39
  • Thank you for your time, do you mean for eg to rather define `x` like `var myVar = 0` due to other loops using the `x` global variable and causing problems? I don't quite understand about where I should I pass `clientReminderCandidates[x]` as a parameter, in the function? I will research function parameters more - thank you again – ZADorkMan Nov 24 '19 at 11:44
  • 1
    Yes, you should use `for (var x = 0; …)` in your loop, and you should use a function parameter for the `clientReminderCandidate` object – Bergi Nov 24 '19 at 18:12
  • Thank you @Bergi, I found a great tutorial to refresh and go through a few times so will attempt my best. Much appreciated. – ZADorkMan Nov 24 '19 at 18:24

0 Answers0