node.js cannot read property of “New”

帅比萌擦擦* 提交于 2019-12-25 09:27:06

问题


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.

My Code looks like this:

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
  }

}

And I always receive the following error:

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

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


回答1:


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"

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

And what you try to do is:

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

Lets get practical

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

if (event.session.new) {

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.

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



来源:https://stackoverflow.com/questions/40834090/node-js-cannot-read-property-of-new

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!