将数据发布到My Alexa Lambda节点js中的MQTT代理后的错误承诺

时间:2017-10-19 12:11:56

标签: javascript node.js aws-lambda alexa alexa-voice-service

我的Lambda存在问题,实际上是在promise node中。我在Lambda中编写了这样的代码:

'use strict'
const Alexa = require('alexa-sdk');
const mqtt = require('mqtt');

const APP_ID = undefined;

const WELCOME_MESSAGE = 'Welcome to the lamp control mode';
const WELCOME_REPROMT = 'If you new please say help'
const HELP_MESSAGE = 'In this skill you can controlling lamp to turn off or on, dim the lamp, change the lamp color and schedule the lamp';
const STOP_MESSAGE = 'Thanks for using this skill, Goodbye!';
const OFF_RESPONSE = 'Turning off the lamp';
const ON_RESPONSE = 'Turning on the lamp';
const DIM_RESPONSE = 'Dimming the lamp';
const CHANGE_RESPONSE = 'Changing the lamp color';
const AFTER_RESPONSE = 'Wanna control something again ?';

const handlers = {
    'LaunchRequest': function () {
        this.emit(':ask', WELCOME_MESSAGE, WELCOME_REPROMT);
    },
    'OnOffIntent' : function () {
        var status = this.event.request.intent.slots.status.value;
        var location = this.event.request.intent.slots.location.value;

        console.log(status);
        console.log(location);

        if (status == 'on') {
            // Promise Start
            var mqttPromise = new Promise(function(resolve, reject) {
                var options = {
                  port: '1883',
                  clientId: 'mqttjs_' + Math.random().toString(16).substr(2, 8),
                  username: 'username',
                  password: 'password',
                };
                var client  = mqtt.connect('mqtt://broker-address', options)
                client.on('connect', function() {
                    client.publish("lamp/status", status + ' ' + location, function() {
                        console.log("Message is published");
                        client.end();
                        resolve('Done Sending');
                    });
                });
            });
            mqttPromise.then(
                function(data) {
                    console.log('Function called succesfully', data);
                    this.emit(':ask', ON_RESPONSE, AFTER_RESPONSE);
                }, function(err) {
                    console.log('An error occurred: ', err);
                }
            );
            // Promise END
            // this.emit(':ask', ON_RESPONSE, AFTER_RESPONSE);
                       // client.publish("lamp/status", status + ' ' + location);
        } else if (status == 'off') {
            this.emit(':ask', OFF_RESPONSE, AFTER_RESPONSE);
            // client.publish("lamp/status", status + ' ' + location);
        }
    },
    'DimIntent' : function () {
        // to do here
    },
    'ChangeColorIntent' : function () {
        // to do here
    },
    'ShceduleIntent' : function () {
        // to do here
    },
    'AMAZON.HelpIntent': function () {
        this.emit(':ask', HELP_MESSAGE, 'Wanna control something ?');
    },
    'AMAZON.StopIntent': function () {
        this.emit(':tell', STOP_MESSAGE);
    }
};

exports.handler = function (event, context, callback) {
    const alexa = Alexa.handler(event, context, callback);
    alexa.APP_ID = APP_ID;
    alexa.registerHandlers(handlers);
    alexa.execute();
}

我在Alexa Developer中使用Service Simulator测试我的代码并获得此结果: Result Image

所以我检查了Lambda中的输出,我得到了这个错误报告: Error in Lamda

任何人都可以帮助我吗?我不知道这是因为这是我的第一次试验:)

1 个答案:

答案 0 :(得分:0)

您的错误的症结在于日志中的这一行:

Cannot read property 'emit' of undefined

按照程序的流程进行之后,很可能会在这里发生:

mqttPromise.then(
  function(data) {
    console.log('Function called succesfully', data);
      // It's probably ocurring in this line below
      this.emit(':ask', ON_RESPONSE, AFTER_RESPONSE);
  }, function(err) {
    console.log('An error occurred: ', err);
  }
)

该日志表明您尝试使用this,它是undefined,并且没有emit属性。那是因为this在Js中是如何工作的。您可以通过保存对此内容的引用来解决此问题

var that = this;
var mqttPromise = new Promise(function(resolve, reject) {
        var options = {
          port: '1883',
          clientId: 'mqttjs_' + Math.random().toString(16).substr(2, 8),
          username: 'username',
          password: 'password',
        };
        var client  = mqtt.connect('mqtt://broker-address', options)
        client.on('connect', function() {
            client.publish("lamp/status", status + ' ' + location, function() {
                console.log("Message is published");
                client.end();
                resolve('Done Sending');
            });
        });
    });
    mqttPromise.then(
        function(data) {
            console.log('Function called succesfully', data);
            that.emit(':ask', ON_RESPONSE, AFTER_RESPONSE);
        }, function(err) {
            console.log('An error occurred: ', err);
        }
    );

我还建议您阅读一下“这在Java中的工作原理”