使用javascript从Parse.com检索图像

时间:2014-06-20 22:46:19

标签: javascript collections parse-platform

我需要你帮助解决这个问题。我试过这样:

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

我以这种方式显示所有属性:

      // 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);
  }
});

但它没有发生任何事情,网页没有加载。当我确实喜欢这段代码,但最后没有网址时,我查看了firebug并查看<img src="[object Object]">如何检索图片?谢谢。

修改

当我使用object.get('photo').url时,我得到undefined

2 个答案:

答案 0 :(得分:0)

我不知道https://www.parse.com/究竟是什么,但我做了一些快速的研究,我认为你必须调用.url()方法:
https://www.parse.com/questions/retrieve-image-using-javascript-api

每当你在字符串中放入某些东西时看到[object Object],就意味着你试图将一个对象变成一个字符串。这里,user.get('photo').url是一个对象,而不是一个字符串。

答案 1 :(得分:0)

我并非100%确定您要做的是什么,但尝试将图片拉回来取决于您的应用环境。

这是针对单张还是多张图片?

您可以使用jQuery在页面上呈现上传的个人资料照片:

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

- 查询具有所需图片的行的内容类 - 获取图片的URL - 设置图像元素的来源

此处有更多信息https://www.parse.com/docs/js_guide#objects-types

示例代码:

 // 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);
      }
    });