2017-06-06 87 views
2

我正在使用Javascript使用contentful-management節點模塊處理Contentful項目。 我想在包含新條目的條目中創建引用。使用參考字段更新條目

我得到在瀏覽器控制檯以下消息:

 Uncaught (in promise) Error: { 
    "request": { 
"url": "https://api.contentful.com:443/spaces/59mi8sr8zemv/entries/2Bxpz2RgA4AQImQOssey8w/published", 
"headers": { 
    "Accept": "application/json, text/plain, */*", 
    "Content-Type": "application/vnd.contentful.management.v1+json", 
    "X-Contentful-User-Agent": "contentful-management.js/1.3.1", 
    "Authorization": "Bearer <my management-api key>", 
    "X-Contentful-Version": 373 
    }, 
    "method": "put", 
    "payloadData": null 
    }, 
    "status": 422, 
    "statusText": "Unprocessable Entity", 
    "requestId": "d00dde60cd564a8db84da90cd671b19f", 
    "message": "Validation error", 
    "details": { 
    "errors": [ 
    { 
    "name": "notResolvable", 
    "link": { 
     "id": "2Yfc2H8q9OSoOccGcSg4aU", 
     "linkType": "Entry", 
     "type": "Link" 
    }, 
    "path": [ 
     "fields", 
     "link", 
     "en-US", 
     7 
    ] 
    } 
] 
} 
} 
at errorHandler (main-addTrack.bundle.js:3096) 

當我看在contentful Web應用程序的新條目是存在的,但保存爲草稿。我試圖添加條目的條目說,它需要更新。

這是我用來創建和更新新條目的功能。

function publishTrack(){ 
     //-- Creates the new track in events, with ref to korrekt date 
     client.getSpace(<my_spaceId>) 
      .then((space) => { 
      space.createEntry('events', newTrack) 
       .then(event => { 

       eventID = event.sys.id; 

       (entry) => entry.publish() 

       //This function is gets the entry of choosen date 
       space.getEntry(dateId) 
        .then((entry) => { 

        //Gets the ID from the newly created event 
        var newId = {sys: { 
         id: eventID, 
         linkType: "Entry", 
         type:"Link" 
        }} 

        //Creates a reference field in dates for show & do 
        entry.fields.link["en-US"].push(newId) 

        //update the event 
        return entry.update() 
        space.getEntry(eventID) 
        .then ((eventID) => entry.publish()) 

       }) 
        space.getEntry(dateId) 
        .then ((entry) => entry.publish()); 

      }) 

      publishModal.style.display = 'block'; 
      publishModal.style.opacity = '1'; 
      publishModal.style.pointerEvents = 'auto'; 
      publishModal.style.zIndex = '99999'; 

     }) 

    }//end publish track 

請爲任何錯誤道歉,這是我在stackowerflow上的第一篇文章。

回答

0

看起來您的某個參考條目無法專門在fields.link["en-US"][7](注意錯誤消息中的「path」屬性)中解析出來,請確保它在創建事件之前已創建併發布。

+0

所以我需要先創建引用字段,然後是條目?但是當我需要參考字段中的條目ID時,我該怎麼做? – Sara

0

在引擎蓋下,您正在執行HTTP PUT請求。這意味着用新的資源替換現有的資源。然而,你的有效載荷爲空:

"payloadData": null

,你會得到一個用戶錯誤的4xx(422特別),這意味着您要發送有效載荷是無效的。因此,這意味着Contentful不接受將該值設爲空。但是,這似乎有點奇怪,因爲您正在添加不爲空的newId

您試圖用PUT替換的資源是/fields/link/en-US/7。可能某些中間資源不存在(例如,/fields,/fields/link/fields/link/en-US),並且子資源上的PUT不由Contentful處理。