Dano007
Dano007

Reputation: 1932

Why wont this return the image from an object to display the image in html?

UPDATED WITH CODE & IMAGE

It looks like the images for each of the returned objects in the query are "undefined"

Parse.com and JavaScript.

I've managed to get my code to return the object I want, now I just want to take out the "pic" url so that I can show it on the html page.

I've looked, but struggle to see examples of how to pull out certain attributes of a class.

The below shows my approach, but thats returning a bad url as shown in the screen shot.

What have I done wrong here?

    var currentUser = Parse.User.current();  
var FriendRequest = Parse.Object.extend("FriendRequest");


    var query = new Parse.Query(FriendRequest);
    query.include('toUser');
    query.equalTo("fromUser", currentUser);
    query.equalTo("status", "Request sent");
    //query.exists("pic");
         query.find({
              success: function(results) {
        // If the query is successful, store each image URL in an array of image URL's
        imageURLs = [];
        for (var i = 0; i < results.length; i++) { 
          var object = results[i];
          imageURLs.push(object.get('pic'));
        }
     // If the imageURLs array has items in it, set the src of an IMG element to the first URL in the array
        for(var j = 0; j < imageURLs.length; j++){
            $('#imgs').append("<img src='" + imageURLs[j] + "'/>");                 
        }
      },
      error: function(error) {
        // If the query is unsuccessful, report any errors
        alert("Error: " + error.code + " " + error.message);
      }
    });
         // show the signup or login page

 </script>
<div id="imgs"></div>

enter image description here

enter image description here

enter image description here

Upvotes: 0

Views: 165

Answers (1)

Robert Rowntree
Robert Rowntree

Reputation: 6289

Descend the stack using "get()" where you have an object as in :

results[0].get("toUser").pic

instead of :

results[0].toUser.pic

In your pic of the debugger, the type of results.0.toUser is "Object".

I dont think you will get a good reference to an object without using the javascript

.get(myObject)

Since you are not using a framework (setting collection and model that make it very easy ) that encapsulates all the specifics of parseing the response , you will have to do it by hand , making sure that each step you descend is done so you have a valid reference/type to what is the child.

The reason you have undefined for results.0.toUser.pic is that you've not done the parse correctly.

Upvotes: 1

Related Questions