2017-03-16 130 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()在執行修補程序請求之前獲取結果。 –

相關問題