How to check whether a collection field (image) is empty?

Hello Wixcode Gurus,
I really wish someone can help me as I’m new to wixcode and I’m liking it so far. Here is the scenario … I have a collection of user profiles and the collection has fields such as first name, last name and profile picture. For every user that logs in and does not have a profile picture, I wish to add a default profile picture (similar to facebook default profile picture . So I’m trying to find a way to check if the profile picture field is empty; in case it is empty, I wish to insert a default profile picture. I will appreciate any code that can resolves this issue. An alternative logic to have this done will also be welcome.

p.s: I’ve done some research about this by reading few wixcode posts and I came up with the code below but it doesn’t work.

if ($w(‘#image1’).src === undefined) {
$w(“#image1”).src = “wix:image://imageURL”;
let img1 = $w(“#image1”).src;
// insert default profile picture
const toInsert = {
“profilePicture”: img1
};
// add the item to the collection
wixData.insert(“User_profile”, toInsert)
.catch( (err) => {
console.log(err);
} );
}

Thanks in advance

Instead of checking that the image is empty, check the value returned from the database.

//Replace
if ($w('#image1').src === undefined)

//with
if(!$w('#dataset1').getCurrentItem.photo)

If you call the database collection directly not using a dataset do the same on the result.
Learn more at wix-dataset API.

Hi Shay,

I’m having a similar issue as above. Going to write out my problem, if you want me to start a new thread let me know.

I have an profile picture upload on my member page, which works great. But I got the error src cant be set to “”, I believe because all of the profile picture categories are blank until a user uploads an image. The error doesnt seem to affect the site’s functioning, but annoys me.

I’ve tried to have an IF function to see if the image url or src is empty, then set it to a url for a placeholder image, and everything works except the IF function triggers even if there is an image in the collection item.

code at the moment…

//first line, console.log, returns an item that includes a https://image source, definitely isnt undefined
console.log($w(‘#currentAthlete18’).getCurrentItem());

//this line runs, I’ve also tried getCurrentItem.profilePic.url and .src ===null and it always runs
if(!$w(‘#currentAthlete18’).getCurrentItem.profilePic){console.log(“IF statement runs”); urlerrorfix();}

urlerrorfix(){
this function just uses setFieldValue “profilePic” “placeholder url”, then saves and refreshes, and it works.
when I exit preview I can see the placeholder image is attached

Also, now if I actually upload an image, profile picture, it triggers the if statement and overwrites it with the placeholder URL, which is obviously not the desired affect…

Any help is appreciated!

I have created a member are, so if a user login and the member area is empty then i want the user to redirect in another page to fill the data. As I am new to Wix can anybody help me out with the code

This post is from 2018 and therefore is being closed.
Please add a new post rather than bumping up from two years ago.

You would benefit from looking at this tutorial here.
https://support.wix.com/en/article/corvid-tutorial-building-your-own-members-area