gpt4 book ai didi

node.js - 变量未从函数中获取返回值

转载 作者:太空宇宙 更新时间:2023-11-04 01:35:09 25 4
gpt4 key购买 nike

我试图将两个函数(send-mail 和sendms)的返回值收集到一个变量(var ordermessage)中。它可以很好地从 send-sms 中获取返回值。

我使用 mailgun api 发送邮件,ordermessage 只收到“未定义”。但发送邮件仍在运行。

我尝试过`await mailgun.messages().send(...)`
我试过 `const myvar = wait mailgun.messages().send(...)`
还有 `let myvar = wait mailgun.messages().send...`

纳达。

我尝试使用一个将 api 调用作为回调的函数。仍然未定义。电子邮件和短信都已发送,但我需要电子邮件服务器的回复。我正在使用 Mailgun 试用版,因此我需要返回回复。

发送邮件.js

var mailgun = require('mailgun-js')({apiKey: process.env.MAILGUN_API_KEY, domain: process.env.MAILGUN_DOMAIN});
var processresponse = "\n";

var data = {
from: 'Zigy Demo Store <admin_demo@zigy.com>',
to: email,
subject: 'You have placed an order.',
text: body
};

console.log("\n----------START EMAIL-------------\n");

mailgun.messages()
.send(data, function (error, body) {
console.log("\nFinally running MAILGUN. Return body is\n", body);
if (body == undefined || body == false) {
console.log("Got nothing from server.");
} else {
processresponse += body.message;
console.log("***********************Gotten reply from Mailgun server.*******************************\n", processresponse);
}
});

订单 Controller 函数

module.exports = {
neworder: async function(req, res) {
var sendemail = require('./send-mail');
var sendtext = require('./send-sms');

var orderdetails = 'New order created at '+ new Date() + '\nItem ---> Price'; //Message that will be sent to users.
var item;
var printcart = await Shoppingcart.findOne({
where: {
id: req.body.cart,
owner: req.body.user
}
}).populate('product');
var ordermessage = '';

for (items in printcart.product) {
item = printcart.product[items];
orderdetails += '\n' + item.name + ' ---> ' + item.price;
}
console.log(orderdetails);
//to get email addr and phone number
phone = req.body.phone;
var email = req.body.email;

var user = await User.findOne({id:printcart.owner});

ordermessage += await sendemail(email, orderdetails);
console.log("\nAfter email, the message is ", ordermessage);

ordermessage += await sendtext(phone, orderdetails);
console.log("\nAfter text, Printing order message to be returned to browser ", ordermessage);

console.log("Final message ", ordermessage);

res.send(ordermessage);
}
};

终端

----------START EMAIL-------------

Calling test function

After email, the message is


Finally running MAILGUN. Return body is
{ id:
'<20190222062410.1.FD7A4868FA0ADF5E@sandbox612cf746219c46ad93d5dc588f9341ff.mailgun.org>',
message: 'Queued. Thank you.' }
***********************Gotten reply from Mailgun server.*******************************

Queued. Thank you.
Checking list of verified numbers...
Found the phonenumber!
You will receive an SMS message with your order details soon.

After text, Printing order message to be returned to browser
You will receive an SMS message with your order details soon.
Final message
You will receive an SMS message with your order details soon.
SM9bc04208f9354834a153fb1ffd7dc8bb

任何帮助将不胜感激。

编辑:我从 send-mail.js 和 send-sms.js 内部调用了 res.write,并删除了变量 ordermessage。

最佳答案

这是一个猜测:我认为 mailgun.messages().send(...) 方法不会返回任何内容,因为它使用经典回调。所以你总是会从中得到undefined

但是你可以从回调中的body参数中获取结果。您可以使用 Promisify将回调转换为 Promise 风格的方法。

const util = require('util');
const fs = require('fs');

const messages = mailgun.messages();

const sendPromisified = util.promisify(messages.send());
sendPromisified(data).then((body) => {
// Do something with `body`
}).catch((error) => {
// Handle mailgun error
});

// Async / await style
try {
const mailBody = await sendPromisified(data);
} catch (err) {
// handle mailgun error `err`
}

关于node.js - 变量未从函数中获取返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54821869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com