完成执行前服务调用 returns 错误网关 - 未等待 Nodemailer 完成
Service call returns bad gateway before it completes execution - Nodemailer not being awaited for completion
我正在尝试从另一个微服务 (Rest API) 加载的 Amazon SQS(队列服务)获取数据。我正在使用此数据中的参数和属性来发送电子邮件。没有等待 nodemailer sendMail 完成它的执行。我在 SO Q&A 中读到 nodemailer 支持回调,现在也等待(如果 Node 版本高于 8)。但是他们两个都不工作。 API 调用在 nodemailer 的 sendMail 执行完成之前返回。正在发送电子邮件,API 完成执行。但是在sendMail完成之前调用返回。
我总是收到 502 错误的网关响应。
下面是发送邮件的代码。
sendMail(body: MailObject): Promise < boolean > {
const senderAddress: Mail.Address = {
address: body.from.emailAddress,
name: body.from.name ? body.from.name : ''
}
const recipientAddress: Mail.Address = {
address: body.to.emailAddress,
name: body.to.name ? body.to.name : ''
}
const mailOptions: Mail.Options = {
from: senderAddress,
to: recipientAddress,
subject: body.subject,
text: body.text ? body.text : undefined,
html: body.html ? body.html : undefined,
amp: body.html ? body.html : undefined
}
return new Promise<any> (async (resolve, reject) => {
if (!this.transporter) {
this.logger.warn(`Transporter not intitialized. Use connect method`);
reject(`Transporter not initialized. Use connect method`);
}
console.log(`[location]: [sendMail][promise]`);
console.log(`[Mail Options]: ${JSON.stringify(mailOptions)}`)
this.transporter.sendMail(mailOptions, (error, data)=>{ //<--- I have also tried await
if(error){
console.error(`[sendMail][error]: ${error}`);
reject(error);
}
else{
console.log(`[sendMail][data]: ${data}`);
resolve(true);
}
})
})
}
请参考以下最终日志片段。返回值的 http 状态为 200。但是在 API 调用 returns 之后记录了 ARC/postman 上的 502 错误网关。
上述响应是在 API 调用在邮递员上显示 502 错误网关后记录的。
正如 Dani 在问题的评论部分中提到的,问题不在上面的代码中。问题是它没有等待服务完成从 SQS 获取数据。
AWS SQS的receiveMessage函数新增promise(第一行代码请参考注释)
this.sqs.receiveMessage(params).promise().then(async data=>{ //<--- Added promise here
console.log(`[MessageBody]: ${data.Messages![0].Body}`);
this.message = {
MessageId : data.Messages![0].MessageId||"",
ReceiptHandle : data.Messages![0].ReceiptHandle||"",
MD5OfBody : data.Messages![0].MD5OfBody||"",
Body : data.Messages![0].Body||"",
MD5OfMessageAttributes : data.Messages![0].MD5OfMessageAttributes||"",
MessageAttributes : {
from: {
StringValue: data.Messages![0].MessageAttributes!.from.StringValue||"",
StringListValues: data.Messages![0].MessageAttributes!.from.StringListValues||{},
BinaryListValues: data.Messages![0].MessageAttributes!.from.BinaryListValues||{},
DataType: data.Messages![0].MessageAttributes!.from.DataType||""
},
subject: {
StringValue: data.Messages![0].MessageAttributes!.subject.StringValue||"",
StringListValues: data.Messages![0].MessageAttributes!.subject.StringListValues||{},
BinaryListValues: data.Messages![0].MessageAttributes!.subject.BinaryListValues||{},
DataType: data.Messages![0].MessageAttributes!.subject.DataType||""
},
to: {
StringValue: data.Messages![0].MessageAttributes!.to.StringValue||"",
StringListValues: data.Messages![0].MessageAttributes!.to.StringListValues||{},
BinaryListValues: data.Messages![0].MessageAttributes!.to.BinaryListValues||{},
DataType: data.Messages![0].MessageAttributes!.to.DataType||""
}
}
}
我正在尝试从另一个微服务 (Rest API) 加载的 Amazon SQS(队列服务)获取数据。我正在使用此数据中的参数和属性来发送电子邮件。没有等待 nodemailer sendMail 完成它的执行。我在 SO Q&A 中读到 nodemailer 支持回调,现在也等待(如果 Node 版本高于 8)。但是他们两个都不工作。 API 调用在 nodemailer 的 sendMail 执行完成之前返回。正在发送电子邮件,API 完成执行。但是在sendMail完成之前调用返回。
我总是收到 502 错误的网关响应。
下面是发送邮件的代码。
sendMail(body: MailObject): Promise < boolean > {
const senderAddress: Mail.Address = {
address: body.from.emailAddress,
name: body.from.name ? body.from.name : ''
}
const recipientAddress: Mail.Address = {
address: body.to.emailAddress,
name: body.to.name ? body.to.name : ''
}
const mailOptions: Mail.Options = {
from: senderAddress,
to: recipientAddress,
subject: body.subject,
text: body.text ? body.text : undefined,
html: body.html ? body.html : undefined,
amp: body.html ? body.html : undefined
}
return new Promise<any> (async (resolve, reject) => {
if (!this.transporter) {
this.logger.warn(`Transporter not intitialized. Use connect method`);
reject(`Transporter not initialized. Use connect method`);
}
console.log(`[location]: [sendMail][promise]`);
console.log(`[Mail Options]: ${JSON.stringify(mailOptions)}`)
this.transporter.sendMail(mailOptions, (error, data)=>{ //<--- I have also tried await
if(error){
console.error(`[sendMail][error]: ${error}`);
reject(error);
}
else{
console.log(`[sendMail][data]: ${data}`);
resolve(true);
}
})
})
}
请参考以下最终日志片段。返回值的 http 状态为 200。但是在 API 调用 returns 之后记录了 ARC/postman 上的 502 错误网关。
上述响应是在 API 调用在邮递员上显示 502 错误网关后记录的。
正如 Dani 在问题的评论部分中提到的,问题不在上面的代码中。问题是它没有等待服务完成从 SQS 获取数据。
AWS SQS的receiveMessage函数新增promise(第一行代码请参考注释)
this.sqs.receiveMessage(params).promise().then(async data=>{ //<--- Added promise here
console.log(`[MessageBody]: ${data.Messages![0].Body}`);
this.message = {
MessageId : data.Messages![0].MessageId||"",
ReceiptHandle : data.Messages![0].ReceiptHandle||"",
MD5OfBody : data.Messages![0].MD5OfBody||"",
Body : data.Messages![0].Body||"",
MD5OfMessageAttributes : data.Messages![0].MD5OfMessageAttributes||"",
MessageAttributes : {
from: {
StringValue: data.Messages![0].MessageAttributes!.from.StringValue||"",
StringListValues: data.Messages![0].MessageAttributes!.from.StringListValues||{},
BinaryListValues: data.Messages![0].MessageAttributes!.from.BinaryListValues||{},
DataType: data.Messages![0].MessageAttributes!.from.DataType||""
},
subject: {
StringValue: data.Messages![0].MessageAttributes!.subject.StringValue||"",
StringListValues: data.Messages![0].MessageAttributes!.subject.StringListValues||{},
BinaryListValues: data.Messages![0].MessageAttributes!.subject.BinaryListValues||{},
DataType: data.Messages![0].MessageAttributes!.subject.DataType||""
},
to: {
StringValue: data.Messages![0].MessageAttributes!.to.StringValue||"",
StringListValues: data.Messages![0].MessageAttributes!.to.StringListValues||{},
BinaryListValues: data.Messages![0].MessageAttributes!.to.BinaryListValues||{},
DataType: data.Messages![0].MessageAttributes!.to.DataType||""
}
}
}