如何在服务功能中使用 Mongoose 查询数据?

发布于 2025-01-10 04:56:32 字数 1645 浏览 0 评论 0原文

我的节点应用程序中有一项服务,我尝试使用 nodemailer 向客户端发送带有链接的电子邮件。我遇到的问题是,当我尝试在服务中使用 Mongoose 查询收件人电子邮件地址时,我在控制台日志中收到此响应消息 Promise {; } 显然我的电子邮件没有发送,因为我没有返回电子邮件地址。我使用下面的代码作为从我的控制器调用的服务。看起来控制器中的一切都工作正常,因为当我使用测试电子邮件地址作为收件人电子邮件时,消息已发送并且函数没有返回错误。我是否编写了错误的查询电子邮件的函数,或者是否有更好的方法来设置它?

var nodemailer = require('nodemailer');
const Obrf = require("../models/Obrf");

const sendLink = (x) => {
    var id = x;
    const offerid = id.toString();
    const url = 'http://localhost:3000/offerletter/' + offerid
    
    async function returnEmail (y) {
        const email = await Obrf.findById(y, 'email_address').exec();
        return email;
    };

    const sendToEmail = returnEmail(id);

    console.log(sendToEmail);

    var transporter = nodemailer.createTransport({
        service: 'gmail',
        auth: {
          user: '[email protected]',
          pass: 'password'
        }
      });
      
      var mailOptions = {
        from: '[email protected]',
        to: sendToEmail,
        subject: 'Congratulations on your offer!',
        text: 'Go to: ' + url + ' to sign your offer letter!'
      };
      
      transporter.sendMail(mailOptions, function(error, info){
        if (error) {
          console.log(error);
        } else {
          console.log('Email sent: ' + info.response);
        }
      });
};

module.exports = {
    sendLink,
  };

I have a service in my node application where I am trying to use nodemailer to send an email with a link to clients. The issue I am having is that when I try to query the recipient email address using Mongoose in the service I get this response message in the console log Promise { <pending> } and obviously my email does not send because I am not returning an email address. I am using the code below as the service that is called from my controller. It looks like everything is working fine in the controller because when I use a test email address as the recipient email the message is sent and function returns no errors. Am I writing the function to query the email wrong or is there a better way I should be setting this up?

var nodemailer = require('nodemailer');
const Obrf = require("../models/Obrf");

const sendLink = (x) => {
    var id = x;
    const offerid = id.toString();
    const url = 'http://localhost:3000/offerletter/' + offerid
    
    async function returnEmail (y) {
        const email = await Obrf.findById(y, 'email_address').exec();
        return email;
    };

    const sendToEmail = returnEmail(id);

    console.log(sendToEmail);

    var transporter = nodemailer.createTransport({
        service: 'gmail',
        auth: {
          user: '[email protected]',
          pass: 'password'
        }
      });
      
      var mailOptions = {
        from: '[email protected]',
        to: sendToEmail,
        subject: 'Congratulations on your offer!',
        text: 'Go to: ' + url + ' to sign your offer letter!'
      };
      
      transporter.sendMail(mailOptions, function(error, info){
        if (error) {
          console.log(error);
        } else {
          console.log('Email sent: ' + info.response);
        }
      });
};

module.exports = {
    sendLink,
  };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山川志 2025-01-17 04:56:32

函数之前的 async 使函数返回 Promise:

您确定 Obrf.findById(y, 'email_address').exec() 返回 Promise 吗?

如果不删除 aysnc 等待,

  function returnEmail (y) {
        const email =  Obrf.findById(y, 'email_address').exec();
        return email;
    };

    const sendToEmail = returnEmail(id);

如果它返回承诺,则执行类似的操作

 const sendToEmail = returnEmail(id).then(id=>id);

async before a function makes the function return a promise:

are you sure if Obrf.findById(y, 'email_address').exec() returning promise ?

if not remove the aysnc await

  function returnEmail (y) {
        const email =  Obrf.findById(y, 'email_address').exec();
        return email;
    };

    const sendToEmail = returnEmail(id);

if its returning the promise then do something like this

 const sendToEmail = returnEmail(id).then(id=>id);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文