Landon G
Landon G

Reputation: 839

"TypeError: Cannot read property 'get' of undefined" for contextual data

I'm running the code that Google/ dialogflow provided on their website (Link) and for some reason it keeps throwing this error everytime I trigger the intent I'm wanting to invoke:

TypeError: Cannot read property 'get' of undefined

Here's the code I have:

app.intent('Make Appointment' , (conv) => {
// This variable needs to hold an instance of Date object that specifies the start time of the appointment.
const dateTimeStart = convertTimestampToDate(conv.parameters.date, 
conv.parameters.time);
// This variable holds the end time of the appointment, which is calculated by adding an hour to the start time.
const dateTimeEnd = addHours(dateTimeStart, 1);
// Convert the Date object into human-readable strings.
const appointmentTimeString = getLocaleTimeString(dateTimeStart);
const appointmentDateString = getLocaleDateString(dateTimeStart);
// The checkCalendarAvailablity() function checks the availability of the time slot.
return checkCalendarAvailablity(dateTimeStart, dateTimeEnd).then(() => {
  // The time slot is available.
  // The function returns a response that asks for the confirmation of the date and time.
  conv.ask(`Okay, ${appointmentDateString} at ${appointmentTimeString}. Did I get that right?`);
}).catch(() => {
  // The time slot is not available.
  conv.add(`Sorry, we're booked on ${appointmentDateString} at ${appointmentTimeString}. Is there anything else I can do for you?`);
  // Delete the context 'MakeAppointment-followup' to return the flow of conversation to the beginning.
  conv.context.delete('MakeAppointment-followup');
});
});



 app.intent('Make Appointment - yes', (conv) => {
// Get the context 'MakeAppointment-followup'
      const context = conv.context.get('MakeAppointment-followup');
// This variable needs to hold an instance of Date object that specifies the start time of the appointment.
const dateTimeStart = convertTimestampToDate(context.parameters.date, context.parameters.time);
// This variable holds the end time of the appointment, which is calculated by adding an hour to the start time.
const dateTimeEnd = addHours(dateTimeStart, 1);
// Convert the Date object into human-readable strings.
const appointmentTimeString = getLocaleTimeString(dateTimeStart);
const appointmentDateString = getLocaleDateString(dateTimeStart);
// Delete the context 'MakeAppointment-followup'; this is the final step of the path.
conv.context.delete('MakeAppointment-followup');
// The createCalendarEvent() function checks the availability of the time slot and marks the time slot on Google Calendar if the slot is available.
return createCalendarEvent(dateTimeStart, dateTimeEnd).then(() => {
  conv.add(`Got it. I have your reservation scheduled on ${appointmentDateString} at ${appointmentTimeString}!`);
}).catch(() => {
  conv.add(`Sorry, something went wrong. I couldn't book the ${appointmentDateString} at ${appointmentTimeString}. Is there anything else I can help you with?`);
});
});

(sorry for the long code, but I feel it's necessary for the question)

All these two functions do is makes an appointment (or booking) and adds a confirmation step to it. So for example, it would look something like this if all went well:

User: I'd like to make an appointment
bot: Sure, what day works for you?
User: Tomorrow
bot: what time?
User: At 6 pm
bot: Okay, February 11 at 6pm, did I get that right?
User: Yes   // Part where it stops working
Bot: Great, I have your appointment booked for February 11 at 6pm!  // App crashes at this part

I'm suspect (although highly doubtful) that the reason could be that in the examples they were using "agent" and the old syntax, where as I'm using the AOG syntax.

Any suggestions/ help are much appreciated!

Upvotes: 0

Views: 995

Answers (1)

Prisoner
Prisoner

Reputation: 50731

You're pretty close.

These should be the property contexts with an "s" at the end.

It looks like you're using the actions-on-google library, which typically has a conv object and uses the contexts property to access the contexts with get(), set(), and delete() methods.

If you used the dialogflow-fulfillment library (which you didn't), it tends to have an agent object and use the context property (without the "s") to access similar get(), set(), and delete() methods.

Upvotes: 3

Related Questions