2017-03-16 131 views
0

我正尝试使用JavaScript更新Google日历中的事件,并且似乎无法实现我指定的特定变量的简单更新。下面的代码是什么我远连同其他类似的组合试过这样:Google Calendar API中的更新事件JavaScript

  var event = {}; 

      // Example showing a change in the location 
      event = {"location": "New Address"}; 

      var request = gapi.client.calendar.events.update({ 
       'calendarId': 'primary', 
       'eventId': booking.eventCalendarId, // Event ID stored in database 
       'resource': event 
      }); 

      request.execute(function (event) { 
       console.log(event); 
      }); 

无论API Reference我已经尝试遵循的,我已经试过让事件本身并通过该引用尝试更新特定变量。但是,使用上面最接近的工作示例代码,我注意到控制台建议我将开始和结束日期作为最小参数。我显然可以将它添加到“事件”对象,但这不是有效的,因为我只希望更新我指定的字段。唯一的另一种选择是简单地删除事件并创建一个新事件 - 必须有一个更简单的方法,或者我完全错过了某些东西。

回答

2

自己解决这个问题,如果有人碰巧遇到同样的问题。正确的格式是使用'PATCH'与'UPDATE',它允许更新特定字段,而不必设置'UPDATE'所需的最小字段范围。

这是正确的代码,我发现,解决了这一问题为例,包括首先的获取事件略有初始编辑:

  var event = gapi.client.calendar.events.get({"calendarId": 'primary', "eventId": booking.eventCalendarId}); 

      // Example showing a change in the location 
      event.location = "New Address"; 

      var request = gapi.client.calendar.events.patch({ 
       'calendarId': 'primary', 
       'eventId': booking.eventCalendarId, 
       'resource': event 
      }); 

      request.execute(function (event) { 
       console.log(event); 
      }); 
+0

此代码的工作,但events.get是一个承诺,您应该使用.then()在执行修补程序请求之前获取结果。 –

相关问题