Error with addproductMedia

Hello, i did see one subject only on that : BUG? wix-stores-backend => addProductMedia() | Velo by Wix

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 , there is an error triggering in the console log :

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 !

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
    }

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.

i modify my post ! Sorry Yisrael san …

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.

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

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


const src=galleryPictures[0].src

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

addProductMedia(newProduct,mediaData);

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) :

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

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

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

**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 ) 
    } 

}

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