0

I've written an if statement that checks if there is a current user and if so, grab that currentUser's profile image url from my DB and place it in a div. I've successfully done so with the following logic:

  function checkLogin() {

    if (Parse.User.current()) {
      var profilePicUrl = Parse.User.current().get("profile_picture").url();

      $(".user-image").css("background-image", "url("+profilePicUrl+")");
    } else {

      $(".user-image").css("background-image", "url(//default image)");
    }
  };

However the issue is, when there is a user that doesn't have a profile image, it throws an error. I've attempted to offset this by adding the following else if statement between the original two:

 function checkLogin() {

    if (Parse.User.current()) {
      var profilePicUrl = Parse.User.current().get("profile_picture").url();

      $(".user-image").css("background-image", "url("+profilePicUrl+")");

    } else if (Parse.User.current() && !profilePicUrl){

      $(".user-image").css("background-image", "url(//default image)");

    } else {

      $(".user-image").css("background-image", "url(//default image)");

    }
  };

Now I'm almost certain I'm using the && operator wrong here, but what I'm trying to say in the middle else if is "if there is a current user & they don't have a profile image ..."

How do I properly state this?

John Durand
  • 1,934
  • 5
  • 22
  • 34

3 Answers3

3

Actually, since parseObject.get() returns null if the key is not found, all you need here is:

 function checkLogin() {
    if (user = Parse.User.current()) {
      var profilePic = user.get("profile_picture");
      var profilePicUrl = profilePic ? profilePic.url() : "//default image";  
      $(".user-image").css("background-image", "url("+profilePicUrl+")");
    }
  };

Note that the var someVar = someVar ? valueA : valueB; syntax is a Conditional (ternary) Operator

7urkm3n
  • 6,054
  • 4
  • 29
  • 46
Wesley Smith
  • 19,401
  • 22
  • 85
  • 133
1

You need to get the profile pic url before checking it and add a try/catch as a safety net (since I don't know the implementation of current and url methods)

function checkLogin() 
{
    try
    {
      var profilePicUrl = Parse.User.current().get("profile_picture").url();
      if (Parse.User.current() && profilePicUrl) 
      {
        $(".user-image").css("background-image", "url("+profilePicUrl+")");
      } 
      else 
      {
        $(".user-image").css("background-image", "url(//default image)");
      }
    }
    catch(e)
    {
      console.log("unexpected error occured");
      $(".user-image").css("background-image", "url(//default image)");
    }
  };
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • `.get("profile_picture").url()` will throw an error when get returns `null`, I know you put the try catch there to protect against that, but worth metioning – Wesley Smith Mar 26 '16 at 07:25
  • @DelightedD0D yes since implementation of url and current methods are not shared. – gurvinder372 Mar 26 '16 at 07:29
-1

try this .....

var profilePicUrl = null;//assign some empty value here...

if (Parse.User.current()) {
      profilePicUrl = Parse.User.current().get("profile_picture").url();   

 } 
if(Parse.User.current() && profilePicUrl ){

     $(".user-image").css("background-image", "url("+profilePicUrl+")");
}
else if (Parse.User.current() && !profilePicUrl){

     $(".user-image").css("background-image", "url(//default image)");

} else {
      $(".user-image").css("background-image", "url(//default image)");
}
mathi
  • 107
  • 1
  • 11