多个日期条目

时间:2019-06-19 22:37:10

标签: javascript node.js firebase dialogflow

这里有一些意向和实体的图像

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

我正在从用户那里收集日期以创建Google日历事件,但是一旦我添加了第二个日期的代码(即使它没有使用),它就无法在两个日期中的任何一个上起作用 它在firebase中给出了此错误:

  

TypeError:无法读取未定义的属性“ split”       在exports.dialogflowFirebaseFulfillment.functions.https.onRequest(/srv/index.js:38:68)       在cloudFunction(/srv/node_modules/firebase-functions/lib/providers/https.js:37:41)       在/worker/worker.js:783:7       在/worker/worker.js:766:11       在_combinedTickCallback(内部/进程/next_tick.js:132:7)       在process._tickDomainCallback(internal / process / next_tick.js:219:9)

这是我的代码

   function makeAppointment2 (agent) {
   const dateTimeStart = new Date(Date.parse(agent.parameters.date.split('T')[0] + 'T' + agent.parameters.time.split('T')[1].split('+')[0] + timeZoneOffset));
   const dateTimeEnd = new Date(new Date(dateTimeStart).setHours(dateTimeStart.getHours() + 1));
   const dateTimeStart2 = new Date(Date.parse(agent.parameters.date2.split('T')[0] + 'T' + agent.parameters.time2.split('T')[1].split('+')[0] + timeZoneOffset));
   const dateTimeEnd2 = new Date(new Date(dateTimeStart2).setHours(dateTimeStart2.getHours() + 1));
   const appointmentTimeString = dateTimeStart.toLocaleString(
     'en-US',
     { month: 'long', day: 'numeric', hour: 'numeric', timeZone: timeZone }
   );
   const appointmentTimeString2 = dateTimeStart2.toLocaleString(
     'en-US',
     { month: 'long', day: 'numeric', hour: 'numeric', timeZone: timeZone }
   );
   return createCalendarEvent2(dateTimeStart, dateTimeEnd, name, location, dateTimeStart2, dateTimeEnd2).then(() => {
     agent.add(`Great Mr/Mrs ${name}, your appointment has been scheduled on ${appointmentTimeString} & ${appointmentTimeString2}!.`);
     agent.add(`${response2}`);
   }).catch(() => {
     agent.add(`I'm sorry Mr/Mrs ${name}, there are no slots available for ${appointmentTimeString} Please choose another date.`);
   });
 function createCalendarEvent2 (dateTimeStart, dateTimeEnd, appointment_name, location, dateTimeStart2, dateTimeEnd2) {
  return new Promise((resolve, reject) => {
    calendar.events.list({
      auth: serviceAccountAuth,
      calendarId: calendarId,
      timeMin: dateTimeStart.toISOString(),
      timeMax: dateTimeEnd.toISOString()
    }, (err, calendarResponse) => {

      if (err || calendarResponse.data.items.length > 0) {
        reject(err || new Error('Requested time conflicts with another appointment'));
      } else {

        calendar.events.insert({ auth: serviceAccountAuth,
          calendarId: calendarId,
          resource: {
   'summary': appointment_name + "1st" ,
   'description': location,
   'start': {
     'dateTime': dateTimeStart,
   },
   'end': {
     'dateTime': dateTimeEnd,
   },
 } ,
        }, (err, event) => {
          err ? reject(err) : resolve(event);
        }
        );
      }
    });
    calendar.events.list({
      auth: serviceAccountAuth,
      calendarId: calendarId,
      timeMin: dateTimeStart2.toISOString(),
      timeMax: dateTimeEnd2.toISOString()
    }, (err, calendarResponse) => {

      if (err || calendarResponse.data.items.length > 0) {
        reject(err || new Error('Requested time conflicts with another appointment'));
      } else {

        calendar.events.insert({ auth: serviceAccountAuth,
          calendarId: calendarId,
          resource: {
   'summary': appointment_name + "2nd" ,
   'description': location,
   'start': {
     'dateTime': dateTimeStart2,
   },
   'end': {
     'dateTime': dateTimeEnd2,
   },
  } ,
        }, (err, event) => {
          err ? reject(err) : resolve(event);
        }
        );
      }
    });

  });

1 个答案:

答案 0 :(得分:0)

我建议您在实现时提取类似这样的参数:

 function botHandler(agent) {
        let state = agent.parameters["stateName"];
        let uState = state.toUpperCase();
        let answer = "So you live in " + uState + "?";

        agent.add(answer);
    }

您可以通过以下方式检查参数是否存在:

if (agent.parameters["stateName"]) {
    //do stuff here
}

我认为它崩溃了,因为没有意图。