laravel 无法读取未定义的属性“hasTime”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35009184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:06:30  来源:igfitidea点击:

Cannot read property 'hasTime' of undefined

javascriptjquerylaravelfullcalendarlaravel-5.1

提问by James

I'm using FullCalendar to display staff hours on a Calendar.

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

I'm pulling the events via an ajax call like so:

我通过 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)
  })
}

This works perfectly fine, however I am getting an error showing in my console "Uncaught TypeError: Cannot read property 'hasTime' of undefined" and that this is coming from fullcalendar.min.js:6.

这工作得很好,但是我的控制台中显示错误“未捕获的类型错误:无法读取未定义的属性 'hasTime'”,并且这是来自fullcalendar.min.js:6.

I'm not very fluent in JavaScript, but my searching suggests that I either haven't provided the right datesor have junk datain there.

我对 JavaScript 不是很流利,但我的搜索表明我要么没有提供正确的日期,要么在那里有垃圾数据

As far as I can tell I am providing all the right data. The function generating the data looks like so:

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

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);
}

which outputs:

输出:

[{"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}]

Can anyone spot what I am doing wrong? I am simply trying to use this to render my events for the given month.

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

Edit: output of console.log(data)

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

It prints out:

它打印出:

[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]

Opening this up I get:

打开这个我得到:

0: Object

0: Object

Opening this up I get:

打开这个我得到:

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

回答by sebas07be

It seems to be you are giving to fullCalendarwrong event's parameter,

似乎是你给了fullCalendar错误的事件参数,

Try to render some manually events first.

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

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
}]

After that, make sure your events match with fullCalendar expected params.

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

回答by James

I couldn't figure out what was going wrong with the above code, however I got around it by using a JSON feed instead:

我无法弄清楚上面的代码出了什么问题,但是我通过使用 JSON 提要来解决它:

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

回答by Henrik

I got this bug, with the following function

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

var postToServerAjax = function(event, delta, revertFunc)
    {
      $.post('/url', {event: event}, function(data)
      {

      }, 'json').fail(function()
      {
        revertFunc();
        alert('We got an error.');
      });
    };

And I found it was because I tried to pass the event on to the post() function. It didn't matter if I changed the name, as soon as I passed it along, I think it tried to serialize it, and it resulted in that error. Now I'm manually specifying and object, where I "clone" the relevant id, start and end over, as I don't need anything else anyway.

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

回答by Nam Lê Quy

Use: JSON.parse(data)in reponse ajax.

用途:JSON.parse(data)响应ajax。