Hi everyone,
The problem: UGC images uploaded into a dataset use the Media Manager URL. It starts with something like : image://v1/ * , and ends with * ~mv2.jpg (or .png, etc.)
The Media Manager URL works for many things within the Wix environment, but doesn't transfer into other uses. For that you need a web URL (https://static.wixstatic.com/media/ *).
You can right click each image to get that info, but who has the time for that? And code is supposed to do those fancy things for you anyway.
The solution: The code below works to convert the Media Manager URL into a web URL then insert the web URL into a dataset. Since users may upload a variety of file types, the code addresses that as well.
In the Backend, create a data.js file and insert the following for a before insert hook:
import wixData from 'wix-data'; import { mediaManager } from 'wix-media-backend'; export function yourDatasetName_beforeInsert(item, context) { let stripped = item.photo.replace('image://v1/', ''); item.imageUrl = 'https://static.wixstatic.com/media/' + stripped.substring(0, stripped.indexOf('/')); return item; }) }
The credit: I am way too green of a coder to have accomplished this. Credit goes to my nephew-in-law who is my hero.
Moving this to Tips, Tutorials and Examples. Thanks for the contribution!