# Error with addproductMedia

**URL:** <https://forum.wixstudio.com/t/error-with-addproductmedia/35779>\
**Category:** Ask the community\
**Tags:** wix-stores, code, question\
**Created:** [August 3, 2021, 2:27pm UTC](https://forum.wixstudio.com/t/error-with-addproductmedia/35779 "2021-08-03T14:27:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![edouard](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/edouard/32/26240_2.png) [@edouard](https://forum.wixstudio.com/u/edouard)\
**Post date:** [August 3, 2021, 2:27pm UTC](https://forum.wixstudio.com/t/error-with-addproductmedia/35779/1 "2021-08-03T14:27:22Z")

</div>

Hello, i did see one subject only on that : [BUG? wix-stores-backend =\> addProductMedia() | Velo by Wix](https://www.wix.com/velo/forum/coding-with-velo/bug-wix-stores-backend-addproductmedia)

But there seems a bug when trying to add media to a product. You can’t using “mainMedia” or “mediaItems” as a variable in your product creation, and i don’t see a way to put media on created products…

While using the API exemple here : [addProductMedia - Velo API Reference - Wix.com](https://www.wix.com/velo/reference/wix-stores-backend/addproductmedia) , there is an error triggering in the console log :

 ![](https://us1.discourse-cdn.com/wix/original/3X/6/7/6777e365d3020771b8f5f799ca4c33f3dc59c861.png)

I saw @yisrael-wix reacting on the post mentionned up there, and there seems like the API reference is the same…

Any thought on that ?

here is the code !

```javascript
import {
    createProduct,
    addProductsToCollection,
    addProductMedia
} from 'backend/products';

$w.onReady(function () {
    /* $w("#addProductButton").onClick(async () => {

      */
})

let galleryPictures = [];

export async function galleryUploadBtn_change(event) {

    let imageToGallery = {};
    let file = $w('#galleryUploadBtn').value;
    if (file.length > 0) {
        const response = await $w('#galleryUploadBtn').startUpload($w('#galleryUploadBtn').buttonLabel = 'Chargement ...');

        if (response) {
            $w('#galleryUploadBtn').buttonLabel = 'Images';
            imageToGallery.src = response.url;
            imageToGallery.type = 'image';
            console.log('gallery', galleryPictures);
            galleryPictures.push(imageToGallery);
            imageToGallery = {};
        }
    }
    galleryDisplayer();
}

function galleryDisplayer() {
    $w('#innGallery').items = galleryPictures;
    $w('#Gallery').expand();
}

export async function videoUploadBtn_change(event) {
    let videoToGallery = {};
    let file = $w('#videoUploadBtn').value;
    if (file.length > 0) {
        const response = await $w('#videoUploadBtn').startUpload($w('#videoUploadBtn').buttonLabel = 'Chargement ...');

        if (response) {
            $w('#videoUploadBtn').buttonLabel = 'Videos';
            videoToGallery.src = response.url;
            videoToGallery.type = 'video';
            console.log('gallery', galleryPictures);
            galleryPictures.push(videoToGallery);
            videoToGallery = {};
        }
    }
    galleryDisplayer();
}

export function clear_click(event) {
    galleryPictures = [];
    galleryDisplayer();
}
    
export async function addProductButton_click(event) {

    console.log("clicked")
    const product = {
        "name": $w("#input1").value,
        "description": $w("#textBox1").value,
        "price": $w("#input2").value,
       // "mainMedia" : galleryPictures[0].src,
      // "mediaItems": galleryPictures,
        "productOptions": {
            "Color": {
                "choices": [{
                        "description": "Black",
                        "value": "Black"
                    },
                    {
                        "description": "Blue",
                        "value": "Blue"
                    }
                ]
            }
        },
        
        "manageVariants": true,
        "productType": "physical",
    }

    try {
        let newProduct = await createProduct(product);

        // The product was created. Now assign it
        // to a collection. Convert the product ID
        // to an array.
        const newProducts = [newProduct._id];
        const collectionId = '95a1eef4-7a2b-4674-ec21-a2361683269c'

        addProductsToCollection(collectionId, newProducts);

        // The product was assigned to a collection.
        // Now let's add media.
         const option = "Color"
         const choice = "Black"
         const src = galleryPictures[0].src

        const mediaData = [{'src':src}]
        console.log(mediaData)
 /* // If a choice and option are defined,
              // addProductMedia() adds media to choice
              if (choice !== "" && option !== "") {
                mediaData[0].choice = {
                  choice,
                  option
                }
              }
*/
        addProductMedia(newProduct, mediaData);
    } catch (err) {
        // handle the error
    }

```

---

<div class="post-metadata">

**Author:** ![yisrael-wix](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/yisrael-wix/32/1107_2.png) [@yisrael-wix](https://forum.wixstudio.com/u/yisrael-wix)\
**Post date:** [August 3, 2021, 2:35pm UTC](https://forum.wixstudio.com/t/error-with-addproductmedia/35779/2 "2021-08-03T14:35:00Z")

</div>

You’ll need to provide more information in order for us to assist. Post your code in a code block so we can see what you are doing.

---

<div class="post-metadata">

**Author:** ![edouard](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/edouard/32/26240_2.png) [@edouard](https://forum.wixstudio.com/u/edouard)\
**Post date:** [August 3, 2021, 2:36pm UTC](https://forum.wixstudio.com/t/error-with-addproductmedia/35779/3 "2021-08-03T14:36:26Z")

</div>

i modify my post ! Sorry Yisrael san …

---

<div class="post-metadata">

**Author:** ![yisrael-wix](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/yisrael-wix/32/1107_2.png) [@yisrael-wix](https://forum.wixstudio.com/u/yisrael-wix)\
**Post date:** [August 3, 2021, 2:45pm UTC](https://forum.wixstudio.com/t/error-with-addproductmedia/35779/4 "2021-08-03T14:45:04Z")

</div>

The functions addProductsToCollection and addProductMedia return Promises which you will need to handle. Adding an **await** before each should be OK. It could be that addProductsToCollection hasn’t finished yet, and therefore you are trying to addProductMedia to a product that hasn’t yet been added to the collection.

---

<div class="post-metadata">

**Author:** ![edouard](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/edouard/32/26240_2.png) [@edouard](https://forum.wixstudio.com/u/edouard)\
**Post date:** [August 3, 2021, 2:52pm UTC](https://forum.wixstudio.com/t/error-with-addproductmedia/35779/5 "2021-08-03T14:52:21Z")

</div>

Hmm, well it worked to stop the error, but doesn’t add the images either way :o

---

<div class="post-metadata">

**Author:** ![edouard](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/edouard/32/26240_2.png) [@edouard](https://forum.wixstudio.com/u/edouard)\
**Post date:** [August 5, 2021, 6:43pm UTC](https://forum.wixstudio.com/t/error-with-addproductmedia/35779/6 "2021-08-05T18:43:58Z")

</div>

Hello @yisrael-wix , i still can’t seems to upload photos with the addproductmedia functions, is there a problem here :

```javascript

const src=galleryPictures[0].src

const mediaData=[{'src':src}]

addProductMedia(newProduct,mediaData);

```

---

<div class="post-metadata">

**Author:** ![edouard](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/edouard/32/26240_2.png) [@edouard](https://forum.wixstudio.com/u/edouard)\
**Post date:** [August 22, 2021, 2:46pm UTC](https://forum.wixstudio.com/t/error-with-addproductmedia/35779/7 "2021-08-22T14:46:20Z")

</div>

Ok i found my answer by breaking the code a bit !

To add multiples images, or even only one use and array mapped (mapping a gallery for example, or a set of images) :

```javascript
innGallery = galleryPictures.map(a => ({ src: a.src })
        const mediaData = innGallery

```

---

<div class="post-metadata">

**Author:** ![rogerhunt](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/rogerhunt/32/8746_2.png) [@rogerhunt](https://forum.wixstudio.com/u/rogerhunt)\
**Post date:** [October 17, 2022, 1:02pm UTC](https://forum.wixstudio.com/t/error-with-addproductmedia/35779/8 "2022-10-17T13:02:10Z")

</div>

Hey all, perhaps reviving this. I am getting internal server errors as well, and not sure why.

The image link: “[https://static.wixstatic.com/media/2f1fd3\_68d13f7f6d8a495dab8bf835a6c18c90~mv2.png](https://static.wixstatic.com/media/2f1fd3_68d13f7f6d8a495dab8bf835a6c18c90~mv2.png)”

add image failed: {errorCode: -100, errorDescription: ‘Internal Server Error [request-id: 1ca57a5a-c528-4f7a-9eb7-1538b7e94cab]’, success: false, payload: null, errorGroup: ‘User’}  
createConsoleProxy.ts:47

```js
**async function** createtheproduct ( loopeditem , productitem , wishlist , image ) { 
    
let team = **await** wixData . **get** ( 'Teams' , wishlist.teamId ) 

    function mapsizes ( array ) { 
        **return** array . map (( item ) => { 
            **return** { 
                "description" : item , 
                "value" : item 
            } 
        }) 
    } 

    let sizearray = **await** mapsizes ( productitem.agentEmail ) 
    **await** console . log ( "Sizes Return" , sizearray ) 

    const product = { 
        "name" : productitem.title , 
        "description" : wishlist . Team + " / " + productitem.title , 
        "price" : productitem.price , 
        "productOptions" : { 
            "Size" : { 
                "choices" : sizearray 
            } 
        }, 
        "manageVariants" : **true** , 
        "productType" : "physical" , 
    } 
    **await** console . log ( "Product JSON: " , product ) 
    **try** { 

        let newProduct = **await** createProduct ( product ); 
        **await** console . log ( "NewProduct: " , newProduct ) 
        // The product was created. Now assign it 
        // to a collection. Convert the product ID 
        // to an array. 
        const newProducts = [newProduct._id]; 
        const collectionId = wishlist.collectionId 

        let collret = **await** addProductsToCollection ( collectionId , newProducts ); 
        console . log ( "Colret: " , collret ) 
        // The product was assigned to a collection. 
        // Now let's add media. 

        **try** { 

            const mediaData = [{ src : image }] 

            let medret = **await** addProductMedia ( newProduct , mediaData ); 
            console . log ( "Medret: " , medret ) 
        } catch ( err ) { 
            console . log ( "add image failed: " , err ) 
        } 
    } catch ( err ) { 
        console . log ( "Error in Product Create: " , err ) 
    } 

}
```

---

<div class="post-metadata">

**Author:** ![edouard](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/edouard/32/26240_2.png) [@edouard](https://forum.wixstudio.com/u/edouard)\
**Post date:** [December 26, 2022, 12:04pm UTC](https://forum.wixstudio.com/t/error-with-addproductmedia/35779/9 "2022-12-26T12:04:10Z")

</div>

Found the solution :

Don’t add the whole product to addProductMedia, just pass the product.\_id

@yisrael-wix the documentation API should be updated though
