首页 > 解决方案 > 如何从外部 nodemailer 传输访问 info.response 对象

问题描述

我正在尝试创建已发送邮件的日志,包括其他一些详细信息,并且需要info.response从节点中的邮件传输器访问对象,我尝试了很多事情,包括尝试将其公开info.response为全局变量,但我似乎无法访问它. 我想使用传输器发送邮件,然后info.response在 log.txt 文件的同一行中记录一些条目详细信息和代码。在resInfo传输器外部无法访问,并且clientReminderCandidates[x][1]无法从传输器内部访问阵列。

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();
                        }
                    });
                }
            }

如何访问 info.response 以便在fs.appendfile函数中使用它来构建包含响应代码的日志条目?

标签: javascriptnode.jsfunctionscopenodemailer

解决方案


推荐阅读