4

I am working on a project with Contentful, using the contentful-management node module, with Javascript. I am trying to create an reference in an entry containing a new entry.

I get the following message in the browser console:

     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)

When I look in the contentful web app the new entry is there, but saved as a draft. The entry I am trying to add an entry to, says that it needs to be updated.

This is the function i use to create and update the new entry.

    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

Please apologize for any mistakes, this is my first post on stackowerflow.

Sara
  • 41
  • 1
  • 4

2 Answers2

2

Looks like one of your reference entries can't be resolved specifically the one at fields.link["en-US"][7] (note the "path" property in the error message), make sure it was created and published before you create the event.

Khaled Garbaya
  • 1,479
  • 16
  • 20
  • So I need to create the reference field first and then the entry? But how would I do that when I need the ID from the entry in the reference field? – Sara Jun 06 '17 at 12:34
1

Under the hood, you are doing a HTTP PUT request. This means replacing an existing resource with a new one. However, your payload is empty:

"payloadData": null

and you get a user error 4xx (422 in particular) which means that the payload you are sending is not valid. So it means that Contentful does not accept putting that value to null. However, it seems a bit weird, as you are adding newId which is not null.

The resource you are trying to replace with PUT is /fields/link/en-US/7. It might be that some intermediary resource is not present (eg, /fields, /fields/link, or /fields/link/en-US), and PUT on a sub-resource is not handled by Contentful.

arcuri82
  • 885
  • 1
  • 8
  • 17