我试图在导出处理程序中使用 async.waterfall
,并顺序调用函数。该功能之一与MQTT消息发布有关。在调用函数时,但是当调用MQTT函数时,它只是停止而不调用require('mqtt')。
exports.handler = function(event, context) {
var async = require('async');
async.waterfall([
function(callback) {
retrieveEmailId(apiAccessToken,callback)
},
function(emailId, callback) {
retrieveDeviceDetails(callback)
},
function(deviceDetail, callback) {
publishMsg(callback)
}
], function(err, result) {
if (err) console.log('Error :: n' + err);
});
}
function retrieveEmailId(accessToken, callback) {
var getEmailFromAlexaProfileObj = require('./GetEmailFromAlexaProfile');
getEmailFromAlexaProfileObj.doIt(accessToken, function(returnVal) {
console.log(returnVal);
callback(null, returnVal)
});
}
function retrieveDeviceDetails(callback) {
var getDevcieDetailsObj = require('./GetDevcieDetails');
getDeviceDetailsObj.doIt(null, function(returnVal) {
console.log(returnVal);
callback(null, returnVal)
});
}
function publishMsg() {
var mqtt = require('mqtt');
var options = {
clientId: "xxx",
username: "yyy",
password: "zzz",
clean: true
};
var client = mqtt.connect("mqtt://xxx.com", options)
client.on("connect", function () {
client.publish('xxx/yyy/L1', "1", options);
client.end();
});
}
您是否尝试使用" lambda-local"在本地运行代码?该呼叫的顺序与MQTT的最后一个序列一起工作吗?在lambda中调用" require('mqtt')"时,您注意到了什么?
问题是否在出口之前完成了要求变量。
例如....
var aws = require('aws-sdk');
var async = require('async');
exports.handler =函数(事件,上下文){
.... ...}