如何在服务功能中使用 Mongoose 查询数据?
我的节点应用程序中有一项服务,我尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
函数之前的 async 使函数返回 Promise:
您确定 Obrf.findById(y, 'email_address').exec() 返回 Promise 吗?
如果不删除 aysnc 等待,
如果它返回承诺,则执行类似的操作
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
if its returning the promise then do something like this