node.js无法读取“ New”属性。 [英] node.js cannot read property of "New"

查看:130
本文介绍了node.js无法读取“ New”属性。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决我的Alexa技能问题。我观看并阅读了很多教程,并尝试进行搜索,但是我总是从Node.js收到一条隐秘的错误消息...
我只是尝试发送http请求,所以没有什么复杂的。

I'm trying to resolve an issue with my Alexa Skill right now. I watched and read a lot of tutorials and tried to search for it, but I always get a cryptic Error Message from Node.js... I just try to send a http request, so nothing complicated.

我的代码如下:

var https = require('https')

exports.handler = (event, context) => {

  try {

    if (event.session.new) {
      console.log("NEW SESSION")
    }

    switch (event.request.type) {

      case "LaunchRequest":
        console.log(`LAUNCH REQUEST`)
        context.succeed(
          generateResponse(
            buildSpeechletResponse("Willkommen bei Taasker", true),
            {}
          )
        )
        break;

      case "IntentRequest":
        console.log(`INTENT REQUEST`)

        switch(event.request.intent.name) {
          case "taskernightlighton":
            var endpoint = "" 
            var body = ""
            http.request("https://autoremotejoaomgcd.appspot.com/sendmessage?key=xxxxxxxxxxxxx&message=nightlighton")
            context.succeed(
                 generateResponse(
                    buildSpeechletResponse(`Licht wird von Taasker eingeschaltet`, true),
                    {}
                 )
            )
            break;

          case "taskernightlightoff":
            http.request("https://autoremotejoaomgcd.appspot.com/sendmessage?key=xxxxxxxxxx&message=nightlightoff")
            context.succeed(
                 generateResponse(
                    buildSpeechletResponse(`Licht wird von Taasker ausgeschaltet`, true),
                    {}
                 )
            )
            break;

          default:
            throw "Invalid intent"
        }

        break;

      case "SessionEndedRequest":
        console.log(`SESSION ENDED REQUEST`)
        break;

      default:
        context.fail(`INVALID REQUEST TYPE: ${event.request.type}`)

    }

  } catch(error) { context.fail(`Exception: ${error}`) }

}

// Helpers
buildSpeechletResponse = (outputText, shouldEndSession) => {

  return {
    outputSpeech: {
      type: "PlainText",
      text: outputText
    },
    shouldEndSession: shouldEndSession
  }

}

generateResponse = (speechletResponse, sessionAttributes) => {

  return {
    version: "1.0",
    sessionAttributes: sessionAttributes,
    response: speechletResponse
  }

}

我总是收到以下错误:

{
  "errorMessage": "Exception: TypeError: Cannot read property 'new' of undefined"
}

有人可以给我一个提示,我的JavaScript语法有什么问题吗?我找不到解释此http请求的资源?请帮助...

Can someone give me a hint, what is wrong with my javascript syntax? I cannot find a source explaining this http request? Please help...

推荐答案

我可以告诉您错误消息的含义。该错误消息一点都不是神秘的。

I can tell you what the error message means. The error message is not cryptic at all.

"Exception: TypeError: Cannot read property 'new' of undefined"

显然您有一个对象,该对象的属性为 new 。因此,在不阅读代码的情况下,我现在知道您有一个看起来像 myObject.new 的对象。错误是说 myObject 是未定义的,因此由于没有值,它无法访问 new 中的值。解释器是这样看的:

Apparently you have an object that has the property new. So without reading your code I now know that you have an object that looks something like myObject.new. The error is saying that myObject is undefined and therefore it cannot access the value in new because there is no value. The interpreter looks at it like this:

var myObject = undefined

而您想做的是:

myObject.new 基本上是运行时间为 undefined.new ,这是不可能的。

myObject.new which basically is viewed by the runtime as undefined.new, which is not possible.

让我们切合实际

当我在程序中执行CTRL + F时,我发现您拥有:

When I did CTRL + F in your program I found that you have:

if(event.session.new){

所以我的猜测如下: event 未定义或会话未定义。这给出了错误,并产生以下问题:如何调用导出的处理程序

So my guess is as follows: either event is undefined or session is undefined. That gives the error, which begs the following question: how are you calling the exported handler? You might have an issue there.

另一个问题是: console.log 是否记录 NEW SESSION

Another question is: does console.log log the "NEW SESSION" ?

这篇关于node.js无法读取“ New”属性。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆