0

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;
        });
    });
});
4

1 回答 1

7

嗨,我是 js SDK 的维护者,假设您有一个名为imagemedia 的字段,您可以如何将资产添加到您的条目中。

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()
    });
});
});
于 2017-03-08T12:37:38.890 回答