1

I have been using the Contentful Management Javascript SDK to update entries in my Contentful space and have been able to update simple fields with text no problem.

My issue is that I can't figure out how to update images in my entry; I can upload images to the media section okay, but I haven't been able to assign any images to an entry property.

Is this possible or is this a limitation of Contentful?

Here's my code so far:

client.getSpace(CONTENTFUL_SPACE).then((space) => {
// retrieve my Contentful space

    space.getEntry(CONTENTFUL_ENTRY).then(function (entry) {
    // get a specific entry

        // create a new asset in my Contentful media section
        space.createAssetWithId(RANDOM_ID, {
            fields: {
                file: {
                    'en-GB': { 
                        contentType: 'image/jpeg',
                        fileName: 'test.jpg',
                        upload: 'http://www.example.com/test.jpg'
                    }
                }
            }
        })
        .then(asset => asset.processForAllLocales())
        .then(asset => asset.publish())
        .then(function(asset) {

            // assign uploaded image as an entry field
            // (none of these work:)
            entry.fields["image"]["en-GB"] = asset.sys.id;
            entry.fields["image"]["en-GB"] = asset.fields.file["en-GB"].url;
            entry.fields["image"]["en-GB"] = asset;
        });
    });
});
mmmoustache
  • 2,273
  • 6
  • 41
  • 62

1 Answers1

8

Hi I am the maintainer of js SDK here is how you can add an asset to your entry assuming that you have a field called image of type media.

client.getSpace(CONTENTFUL_SPACE).then((space) => {
// retrieve my Contentful space

space.getEntry(CONTENTFUL_ENTRY).then(function (entry) {
// get a specific entry

    // create a new asset in my Contentful media section
    space.createAssetWithId(RANDOM_ID, {
        fields: {
            file: {
                'en-GB': { 
                    contentType: 'image/jpeg',
                    fileName: 'test.jpg',
                    upload: 'http://www.example.com/test.jpg'
                }
            }
        }
    })
    .then(asset => asset.processForAllLocales())
    .then(asset => asset.publish())
    .then(function(asset) {

        // assign uploaded image as an entry field
        entry.fields["image"]["en-GB"] = {"sys": {"id": asset.sys.id, "linkType": "Asset", "type": "Link"}};
        return entry.update()
    });
});
});
Khaled Garbaya
  • 1,479
  • 16
  • 20
  • Hi Khaled I was wondering how would we grab an entry id, before we're able to post the entry we would like to add an image to? – justkeithcarr Aug 06 '21 at 20:43
  • 1
    If the entry was created yet you can replace the space.getEntry by space.createEntry – Khaled Garbaya Aug 08 '21 at 09:38
  • Hi Khaled. Thanks for your response. I'm trying to add the entry.update() but I'm getting a Version Mismatch error. I read that we need to add X-Contentful-Version in the header. I'm not sure why would that would be, all the while this answer seemed to work out for the OP. – justkeithcarr Aug 30 '21 at 01:34