0

I would like to add an asset along with an entry. I'm using a form to enter data and upload an image using cloudinary. Contentful-Management is giving me a 409 status code and a 'Version Mismatch' error. I first thought it could be from not publishing an entry and causing me to have an outdated version of the entry. But I've went through my code and fixed that specifically.

These are the steps I'm taking below (add any if you've seen a step I've overlooked, it's much appreciated)

#1 Get Space With Id
#2 Get Environment
#3 Create Entry
#4 Reassign Entry to have latest Version
#5 Asset Creation
#6 Asset Publish
#7 Update Entry With New Asset

Here's my error message below:

enter image description here

 async function createEntryWithAsset(
        data: Omit<Property, "id">,
        uploadHref: string
      ) {
        // #1 Get Space
        const space = await client.getSpace(process.env.NEXT_CONTENTFUL_SPACE_ID);
        // #2 Get Environment
        const environment = await space.getEnvironment("master");
    
        // #3 Create Entry
        let entry = await environment.createEntry("propertyListings", {
          fields: {
            /* ... */
          },
        });
        // #4 Reassign Entry to have latest version
        entry = await entry.publish();
    
        // #5 Asset Creation
        let asset = await environment.createAssetWithId(
          faker.datatype.number(4).toString(),
          {
            fields: {
              title: {
                "en-US": "images",
              },
              file: {
                "en-US": {
                  contentType: "image/jpeg",
                  fileName: faker.datatype.number(5) + "-" + data.zipcode + ".jpg",
                  upload: uploadHref,
                },
              },
            },
          }
        );
        // #6 Asset Publish
        asset = await asset.processForAllLocales()
        asset = await asset.publish();
    
          //#7 Update Entry With New Asset
        entry.fields["images"] = {
         'en-US': {
           sys: {
            id: asset.sys.id,
            linkType: "Asset",
            type: "Link",
          }},
        };  
        entry = await entry.update();
        entry = await entry.publish();
      }
4

0 回答 0