我想通过 SendGrid 将邮件发送到我从 sequelize 查询中获得的特定 ID,但邮件无法发送到从 table 获取的邮件
I want to sent mail through SendGrid to a particular id which i get from sequelize query but the mails can not sent to that fetched mail from a table
Leave.create({
leaveType: leaveType,
fromDate: fromDate,
toDate: toDate,
emailId: emailId,
reason: reason,
reportTo: reportTo,
userId: userId
})
LeaveReq.create({
empName: userId,
appliedFrom: fromDate,
apliedTo: toDate,
leaveType: leaveType,
status: req.body.status,
reqTo: reportTo
})
.then(result =>{
transporter.sendMail({
to: EmpList.findOne({attributes:['profEmail'],where:{firstName: reportTo}}),
from: 'CTHRM@coretechies.com,',
subject: 'You recieved a user request',
html: `<p>please check your app to take action on this request</p>
`
});
res.status(201).json({message: 'leave updated', userId: result.id, status: '1'})
})
.catch(err =>{
if(!err.statusCode){
err.statusCode = 500;
}
next(err);
});
我收到这个错误
node:9440) UnhandledPromiseRejectionWarning: Error: Missing destination email
at Request._callback (D:\ct-work\cthrm_api\node_modules\sendgrid\lib\sendgrid.js:88:25)
at Request.self.callback (D:\ct-work\cthrm_api\node_modules\request\request.js:185:22)
at Request.emit (events.js:210:5)
at Request.<anonymous> (D:\ct-work\cthrm_api\node_modules\request\request.js:1161:10)
at Request.emit (events.js:210:5)
at IncomingMessage.<anonymous> (D:\ct-work\cthrm_api\node_modules\request\request.js:1083:12)
at Object.onceWrapper (events.js:299:28)
at IncomingMessage.emit (events.js:215:7)
at endReadableNT (_stream_readable.js:1200:12)
at processTicksAndRejections (internal/process/task_queues.js:80:21)
(node:9440) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function
without a catch block, or by rejecting a promise which was not handled
with .catch(). (rejection id: 1) (node:9440) [DEP0018]
DeprecationWarning: Unhandled promise rejections are deprecated. In
the future, promise rejections that are not handled will terminate the
Node.js process with a non-zero exit code.
建议一些通过 'to' 部分
中的 sql 查询发送邮件的方法
这段代码有两个问题:
1) sendMail
到 属性 支持的类型是 string | string[]
.
即您可以发送 "a@domain, b@domain"
或 ["a@domain", "b@domain"]
。
的返回值
EmpList.findOne({attributes:['profEmail'],where:{firstName: reportTo}});
是 promise<pending>
因为您不是在等待结果。您实际上是将 to
分配给:Promise<pending>
而不是:string | string[]
.
如果您要使用 async
函数,您可以这样做:
const emp = await EmpList.findOne({attributes:['profEmail'],where:{firstName: reportTo}})
transporter.sendMail({
to: emp,
from: 'CTHRM@coretechies.com,',
subject: 'You recieved a user request',
html: `<p>please check your app to take action on this request</p>`
});
res.status(201).json({message: 'leave updated', userId: result.id, status: '1'})
然后将res
分配给sendMail
的to
属性
2) sendMail
returns 也是一个 promise,所以你必须等待 promise 的结果,如果抛出一个错误就捕获错误:
transporter.sendMail({
to: EmpList.findOne({attributes:['profEmail'],where:{firstName: reportTo}}),
from: 'CTHRM@coretechies.com,',
subject: 'You recieved a user request',
html: `<p>please check your app to take action on this request</p>`
})
.then((data) => conosle.log(data))
.catch((e) => console.error(e);
这就是你得到的原因:
(node:9440) UnhandledPromiseRejectionWarning: Unhandled promise
rejection. This error originated either by throwing inside of an async
function without a catch block, or by rejecting a promise which was
not handled with .catch(). (rejection id: 1) (node:9440) [DEP0018]
DeprecationWarning: Unhandled promise rejections are deprecated. In
the future, promise rejections that are not handled will terminate the
Node.js process with a non-zero exit code.
编辑:
具有异步功能的完整示例。
假设所有这些代码都是在函数下编写的,假设这个函数被称为 create
.
async function create(req, res, next) {
try {
let leaveRes = await Leave.create({
leaveType: leaveType,
fromDate: fromDate,
toDate: toDate,
emailId: emailId,
reason: reason,
reportTo: reportTo,
userId: userId
});
let leaveReqRes = await LeaveReq.create({
empName: userId,
appliedFrom: fromDate,
apliedTo: toDate,
leaveType: leaveType,
status: req.body.status,
reqTo: reportTo
});
const emp = await findEmp({firstName: reportTo});
await sendMailToEmp(emp);
// If you made it to here, everything succeeded.
res.status(201).json({message: 'leave updated', userId: result.id, status: '1'});
} catch(e) {
console.error('Problem in executing create', e);
throw e; // or next (e) depends on how it is built.
}
}
function findEmp(filter) {
return EmpList.findOne({
attributes:['profEmail'],
where: filter
});
}
function sendEmailToEmp(emp) {
try {
transporter.sendMail({
to: emp,
from: 'CTHRM@coretechies.com,',
subject: 'You recieved a user request',
html: `<p>please check your app to take action on this request</p>`
});
} catch(e) {
console.error('Problem in sending mail', e);
}
}
Leave.create({
leaveType: leaveType,
fromDate: fromDate,
toDate: toDate,
emailId: emailId,
reason: reason,
reportTo: reportTo,
userId: userId
})
LeaveReq.create({
empName: userId,
appliedFrom: fromDate,
apliedTo: toDate,
leaveType: leaveType,
status: req.body.status,
reqTo: reportTo
})
.then(result =>{
transporter.sendMail({
to: EmpList.findOne({attributes:['profEmail'],where:{firstName: reportTo}}),
from: 'CTHRM@coretechies.com,',
subject: 'You recieved a user request',
html: `<p>please check your app to take action on this request</p>
`
});
res.status(201).json({message: 'leave updated', userId: result.id, status: '1'})
})
.catch(err =>{
if(!err.statusCode){
err.statusCode = 500;
}
next(err);
});
我收到这个错误
node:9440) UnhandledPromiseRejectionWarning: Error: Missing destination email
at Request._callback (D:\ct-work\cthrm_api\node_modules\sendgrid\lib\sendgrid.js:88:25)
at Request.self.callback (D:\ct-work\cthrm_api\node_modules\request\request.js:185:22)
at Request.emit (events.js:210:5)
at Request.<anonymous> (D:\ct-work\cthrm_api\node_modules\request\request.js:1161:10)
at Request.emit (events.js:210:5)
at IncomingMessage.<anonymous> (D:\ct-work\cthrm_api\node_modules\request\request.js:1083:12)
at Object.onceWrapper (events.js:299:28)
at IncomingMessage.emit (events.js:215:7)
at endReadableNT (_stream_readable.js:1200:12)
at processTicksAndRejections (internal/process/task_queues.js:80:21)
(node:9440) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function
without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:9440) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
建议一些通过 'to' 部分
中的 sql 查询发送邮件的方法这段代码有两个问题:
1) sendMail
到 属性 支持的类型是 string | string[]
.
即您可以发送 "a@domain, b@domain"
或 ["a@domain", "b@domain"]
。
EmpList.findOne({attributes:['profEmail'],where:{firstName: reportTo}});
是 promise<pending>
因为您不是在等待结果。您实际上是将 to
分配给:Promise<pending>
而不是:string | string[]
.
如果您要使用 async
函数,您可以这样做:
const emp = await EmpList.findOne({attributes:['profEmail'],where:{firstName: reportTo}})
transporter.sendMail({
to: emp,
from: 'CTHRM@coretechies.com,',
subject: 'You recieved a user request',
html: `<p>please check your app to take action on this request</p>`
});
res.status(201).json({message: 'leave updated', userId: result.id, status: '1'})
然后将res
分配给sendMail
to
属性
2) sendMail
returns 也是一个 promise,所以你必须等待 promise 的结果,如果抛出一个错误就捕获错误:
transporter.sendMail({
to: EmpList.findOne({attributes:['profEmail'],where:{firstName: reportTo}}),
from: 'CTHRM@coretechies.com,',
subject: 'You recieved a user request',
html: `<p>please check your app to take action on this request</p>`
})
.then((data) => conosle.log(data))
.catch((e) => console.error(e);
这就是你得到的原因:
(node:9440) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:9440) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
编辑:
具有异步功能的完整示例。
假设所有这些代码都是在函数下编写的,假设这个函数被称为 create
.
async function create(req, res, next) {
try {
let leaveRes = await Leave.create({
leaveType: leaveType,
fromDate: fromDate,
toDate: toDate,
emailId: emailId,
reason: reason,
reportTo: reportTo,
userId: userId
});
let leaveReqRes = await LeaveReq.create({
empName: userId,
appliedFrom: fromDate,
apliedTo: toDate,
leaveType: leaveType,
status: req.body.status,
reqTo: reportTo
});
const emp = await findEmp({firstName: reportTo});
await sendMailToEmp(emp);
// If you made it to here, everything succeeded.
res.status(201).json({message: 'leave updated', userId: result.id, status: '1'});
} catch(e) {
console.error('Problem in executing create', e);
throw e; // or next (e) depends on how it is built.
}
}
function findEmp(filter) {
return EmpList.findOne({
attributes:['profEmail'],
where: filter
});
}
function sendEmailToEmp(emp) {
try {
transporter.sendMail({
to: emp,
from: 'CTHRM@coretechies.com,',
subject: 'You recieved a user request',
html: `<p>please check your app to take action on this request</p>`
});
} catch(e) {
console.error('Problem in sending mail', e);
}
}