Severus15
Severus15

Reputation: 127

node.js cannot read property of "New"

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...

Upvotes: 0

Views: 1021

Answers (1)

Melvin Roest
Melvin Roest

Reputation: 1492

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" ?

Upvotes: 2

Related Questions