Hi There
OK I made a minor, but important, mistake with the example I provided (which I have corrected in the post above)
The test I proposed:
if (!currentItem.optionalphoto ||
currentItem.optionalphoto.length > 0)
should have been
if (!currentItem.optionalphoto ||
currentItem.optionalphoto.length === 0)
Essentially testing to see if
a. we are missing an optional photo field
or
b. we have an optional photo field BUT the field length is 0 meaning that there is no photo reference
OK now there are a couple of things to look at.
- We need to understand more what you are trying to accomplish and
- a little education regarding images and how they are used.
Let’s start with the second
When you download an image it goes into a special storage area that is referenced by the Wix code framework using a URL that is a string and looks like this:
"wix:image://v1/df0e14_5d4967c2f63e4ffe82a80eb7e52f39dc~mv2_d_1667_1667_s_2.png/df0e14_5d4967c2f63e4ffe82a80eb7e52f39dc~mv2_d_1667_1667_s_2.png#originWidth=1667&originHeight=1667"
When you access the property for optionalphoto in the dataset you are really looking for a string that looks like this.
The Wix API does the hard work for you in terms of determining where your image is and how to reference it using the image type property and creating the string describing the URL.
So if the image string is malformed (not easy to figure out unless you try to use it) or either doesn’t exist or has a length of zero (e.g. the string looks like “”) then you don’t have an image.
You can test this out in your code by adding a log statement that will show up at the bottom of your page when running in preview mode. Try inserting this call before the if test:
console.log('Dumping dataset currentItem...\n'+
JSON.stringify(currentItem));
console.log('Optional Photo: '+currentItem.optionalphoto);
If you continue to have problems then helping you will be easier if you share the page you are having problems with and describe what you are expecting to see on the page.
Hope this helps
Cheers
Steve