【问题标题】:Cannot read property 'hasTime' of undefined无法读取未定义的属性“hasTime”
【发布时间】:2016-05-02 17:47:26
【问题描述】:

我正在使用 FullCalendar 在日历上显示员工时间。

我通过这样的 ajax 调用来拉事件:

"events": function(start, end, timezone, callback) {

  //create the data to be sent
  var objectToSend = {
    "start_date": start.format("YYYY-MM-DD"),
    "finish_date": end.format("YYYY-MM-DD"),
  };

  //craft and make the request
  $.ajax({
    url: 'calendar/test',
    data: objectToSend,
    type: 'POST',
    cache: false
  }).done(function(data) {
    //on success call `callback` with the data
    callback(data)
  })
}

这工作得很好,但是我的控制台中显示一个错误“Uncaught TypeError: Cannot read property 'hasTime' of undefined”,这来自fullcalendar.min.js:6

我的 JavaScript 不是很流利,但我的搜索表明我要么没有提供 right dates,要么没有提供 junk data

据我所知,我提供了所有正确的数据。生成数据的函数如下所示:

public function test(Request $request) {
  $start_date = Input::get('start_date');
  $finish_date = Input::get('finish_date');

  $shifts = Roster::whereBetween('date', array($start_date, $finish_date)) - > get();

  foreach($shifts as $shift) {
    $start = $shift - > date.
    ' '.$shift - > start_time;
    $finish = $shift - > date.
    ' '.$shift - > finish_time;

    $events[] = array(
      'title' => $shift - > staff - > first_name,
      'start' => Carbon::createFromFormat('Y-m-d H:i:s', $start) - > toDateTimeString(),
      'end' => Carbon::createFromFormat('Y-m-d H:i:s', $finish) - > toDateTimeString(),
      'id' => $shift - > id,
      'allDay' => false
    );
  }

  return json_encode($events);
}

哪个输出:

[{"title":"Gemma","start":"2016-02-01 18:00:00","end":"2016-02-01 22:00:00","id":1,"allDay":false},
{"title":"Gemma","start":"2016-01-26 18:00:00","end":"2016-01-26 22:00:00","id":49,"allDay":false}]

谁能发现我做错了什么?我只是想用它来呈现给定月份的事件。

编辑:console.log(data) 的输出

打印出来:

[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]

打开这个我得到:

0: Object

打开这个我得到:

allDay: false
end: "2016-02-01 22:00:00"
id: 1
start: "2016-02-01 18:00:00"
title: "Gemma"

【问题讨论】:

  • 显示callback函数..
  • @AndriyIvaneyko 我只是想关注这个fullcalendar.io/docs/event_data/events_function 不确定回调函数在哪里?除了以 XML 形式提供响应之外,我将其以 JSON 形式提供。
  • 好的,尝试将callback(data)替换为callback(data[0])
  • 能否在done函数中调用回调之前提供console.log(data)
  • @AndriyIvaneyko 刚刚尝试过,我得到了同样的错误。

标签: javascript jquery laravel fullcalendar laravel-5.1


【解决方案1】:

看来你给fullCalendar的事件参数错误,

尝试先手动渲染一些事件。

events: [{
  title: 'event1',
  start: '2010-01-01'
}, {
  title: 'event2',
  start: '2010-01-05',
  end: '2010-01-07'
}, {
  title: 'event3',
  start: '2010-01-09T12:30:00',
  allDay: false // will make the time show
}]

之后,确保您的事件与 fullCalendar 预期参数匹配。

【讨论】:

  • 我之前是手动渲染成功的,但是当我实现这个的时候,我开始遇到麻烦了。
  • 手动提供输出也可以正常工作,即[{"title":"Gemma","start":"2016-02-01 18:00:00","end":"2016-02-01 22:00:00","id":1,"allDay":false}] 成功显示事件。
  • 确保 json_encode 为您提供正确的 json 格式。尝试使用 jQuery.parseJSON() 进行检查,由于某种原因,fullCalendar 不喜欢您在 start 参数或 end 中的数据格式。
  • 如何使用jQuery.parseJSON()
  • 使用您的输出:[{"title":"Gemma","start":"2016-02-01 18:00:00","end":"2016-02-01 22:00:00","id":1,"allDay":false}, {"title":"Gemma","start":"2016-01-26 18:00:00","end":"2016-01-26 22:00:00","id":49,"allDay":false}]; fullCalendar 应该能够解析您的事件,确保您的 callback() 函数已定义并返回 JSON.parse(data) 而不是 data
【解决方案2】:

我不知道上面的代码出了什么问题,但是我通过使用 JSON 提要解决了这个问题:

events: {
            url: 'calendar/test',
            error: function() 
            {
                alert("error");
            },
            success: function()
            {
                console.log("successfully loaded");
            }
        }

【讨论】:

    【解决方案3】:

    我得到了这个错误,具有以下功能

    var postToServerAjax = function(event, delta, revertFunc)
        {
          $.post('/url', {event: event}, function(data)
          {
    
          }, 'json').fail(function()
          {
            revertFunc();
            alert('We got an error.');
          });
        };
    

    我发现这是因为我试图将事件传递给 post() 函数。改名字没关系,一传下去,我想它就想序列化它,结果就是那个错误。现在我手动指定和对象,我“克隆”相关的 id,开始和结束,因为我不需要任何其他东西。

    【讨论】:

      【解决方案4】:

      在响应 ajax 中使用:JSON.parse(data)

      【讨论】:

        猜你喜欢
        • 2016-03-03
        • 1970-01-01
        • 2021-11-07
        • 2017-09-04
        • 1970-01-01
        • 2019-02-14
        • 2020-04-06
        • 2020-10-30
        • 2019-10-09
        相关资源
        最近更新 更多