2017-06-06 75 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处理。