/*******************************
* Backend code - products.jsw *
*******************************/
import wixStoresBackend from 'wix-stores-backend';
export function removeProductMedia(productId, media) {
return wixStoresBackend.removeProductMedia(productId, media);
}
/*************
* Page code *
*************/
import wixData from 'wix-data';
import { removeProductMedia } from 'backend/products';
// ...
const productName = ...; // get name of product
wixData.query("Stores/Products")
.eq("name", productName)
.find()
.then((results) => {
if (results.items.length > 0) {
const productId = results.items[0]._id;
removeProductMedia(productId) // not passing media will remove all media from product
.then(() => {
// all media items removed from the product
})
.catch((error) => {
// media items not removed from the product
});
}
});
I refer to the above example code from the velo document. In the example the media item is not specified. I was trying to write a code with specified media (i.e. with src url) but I cant figure in what format the media para. should be in… all my attempts failed and the command was just ignored. Anyone can give me an example code for that?
I have tried
var productId =
var src = 'www.////'
var mediaData = {src}
removeProductMedia(productId,mediaData)
not working. And none of the below works either.
var mediaData = src
var mediaData = [{src}]
var mediaData = [src]
var mediaData = [{
src
} ]
var mediaData = [{src},,]
Hey @myapps20000 , it looks like you may have just copied the snippet there into only your page code OR backend code. You’ll notice that there are two parts to it, with the comments highlighting each section. Those pieces of code are to go into two separate files (backend/products.jsw and your page code).
The issue you’re probably having is with the imports themselves.
import wixStoresBackend from 'wix-stores-backend'; //This needs to be in a backend file.
can’t work in the page code on the front end, because the modules it requires to work are specific to Wix’s backend environment, running on node.js.
If this doesn’t solve it, what you can do is in the line where you have .catch((error)=>{, add a console.error so you can see the full error message and how to fix it in your console. That would look be like this.
.catch((error)=>{
console.error(error);
});
I have already done that. The example code works just fine with no media specific, which deletes all images of the product. But in the document it says there is a way to specify which media to delete, which I don’t know what to put in the {media} para.