Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I need your help with this question. I tried like this:

var user = new Parse.User();    
var profilePhoto = document.createElement("img");
profilePhoto.setAttribute("src", user.get('photo').url);

I display all properties this way:

      // User Model
      // -----------
      Parse.initialize("id", "id"); 

    var User = new Parse.User(); 

    var TestCollection = Parse.Collection.extend({
        model: Parse.User
    }); 

    var collection = new TestCollection();
        collection.fetch({

        success: function(collection) {                 

                var table = document.createElement("table");
                var thead = document.createElement("thead");
                var tbody = document.createElement("tbody");                
                var tr_header = document.createElement("tr");
                tr_header.setAttribute("class", "header");
                table.setAttribute("class", "dataTable");
                table.setAttribute("id", "user_table");



                var td_header_name = document.createElement("td");
                var td_header_email = document.createElement("td");
                var td_header_num = document.createElement("td");
                var td_header_photo = document.createElement("td");

                var username_label = document.createTextNode("Username"); 
                var email_label = document.createTextNode("Email");
                var num_label = document.createTextNode("№");
                var photo_label = document.createTextNode("Photo");

                td_header_name.appendChild(username_label);
                td_header_email.appendChild(email_label);
                td_header_num.appendChild(num_label);
                td_header_photo.appendChild(photo_label);

                tr_header.appendChild(td_header_num);
                tr_header.appendChild(td_header_name);
                tr_header.appendChild(td_header_email);
                tr_header.appendChild(td_header_photo);
                thead.appendChild(tr_header);

                table.appendChild(thead);                   
                table.appendChild(tbody);

                collection.each(function(user) {

                var tr = document.createElement("tr");   

                var td_num = document.createElement("td");
                var td1 = document.createElement("td");
                var td2 = document.createElement("td");             
                var profilePhoto = document.createElement("td");    

                var number = document.createTextNode(collection.indexOf(user)+1);           
                var username = document.createTextNode(user.get('username')); 
                var email = document.createTextNode(user.get('email'));

                var img = document.createElement("img");
                var img_src = user.get('photo');                

                img.setAttribute("id", "img_user");
                img.setAttribute("src",  user.get('photo').url);

                td_num.appendChild(number);
                td1.appendChild(username);
                td2.appendChild(email);
                profilePhoto.appendChild(img);

                tr.appendChild(td_num);
                tr.appendChild(td1);
                tr.appendChild(td2);
                tr.appendChild(profilePhoto);

                tbody.appendChild(tr);

                table.appendChild(thead);
                table.appendChild(tbody);

                document.getElementById('user_list').appendChild(table);

    });
  },
  error: function(collection, error) {
    alert("Error: " + error.code + " " + error.message);
  }
});

But it happened nothing, the webpage didn't load. When I did like this code, but without url in the end, I checked with firebug and saw <img src="[object Object]">How can I retrieve images? Thank you.

EDIT

When I use object.get('photo').url I get undefined

share|improve this question
    
well a new parse user isn't going to have a photo field. you probably meant Parse.User.current(); – Fosco Jun 20 '14 at 22:50
    
No I retrieve all users using collection and string fields get fine. – Maximus Jun 20 '14 at 22:58
    
The code as you've provided cannot work, you've created an empty Parse.User instead of getting one the correct way (query or Parse.User.current()) and expect it to have a photo property, then call a method url() as if it is a property instead of a method... wrong on so many levels. – Timothy Walters Jun 21 '14 at 6:11
    
Parse.User is not empty. There are many notes. I receive all property except photo. – Maximus Jun 21 '14 at 21:03

2 Answers 2

up vote 0 down vote accepted

I'm not 100% sure what your trying to do, but as far as trying to pull an image back depends on the context of your application.

Is this for a single or multiple image?

You could render an uploaded profile photo on a page with jQuery:

var profilePhoto = profile.get("photoFile"); $("profileImg")[0].src = profilePhoto.url();

-Query the content Class for the row which has the desired picture -Get the picture's URL -Set the source for an image element

More info here https://www.parse.com/docs/js_guide#objects-types

Example code:

 // Query the content class for rows where the image exists
    var Content = Parse.Object.extend("content");
    var query = new Parse.Query(Content);
    query.exists("image");
    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('image'));
        }
        // If the imageURLs array has items in it, set the src of an IMG element to the first URL in the array
        if(imageURLs.length > 0){
          $('#color').attr('src', imageURLs[0]);
        }
      },
      error: function(error) {
        // If the query is unsuccessful, report any errors
        alert("Error: " + error.code + " " + error.message);
      }
    });
share|improve this answer
    
At first, I don't retrieve Content, Person, Anybody else object. I want retrive from User object, photo field (it has File type there). These are for user's images, but I want retrieve even only one image. – Maximus Jun 23 '14 at 9:54
    
I read this doc, but it didn't help me. – Maximus Jun 23 '14 at 9:56

I have no idea what https://www.parse.com/ actually is, but I did some quick research and I think you have to call the .url() method:
https://www.parse.com/questions/retrieve-image-using-javascript-api

Whenever you see [object Object] when you put something in a string, it means you tried to turn an object into a string. Here, user.get('photo').url is an object, not a string.

share|improve this answer
    
:)It's good but I tried this, it was the same effect(nothing work). – Maximus Jun 20 '14 at 23:13
    
Oh...What happened when you called .url? Did a TypeError come up? @Maximus – Noble Mushtak Jun 20 '14 at 23:15
    
Nothing happened...The webpage was empty. – Maximus Jun 21 '14 at 20:59
    
Hm...Well, good luck then! I hope someone with a better understanding of this Parse API can help you! – Noble Mushtak Jun 23 '14 at 0:06
    
Thanx, but it's not help me. – Maximus Jun 23 '14 at 9:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.