【问题标题】:Retrieving images from Parse.com using javascript使用 javascript 从 Parse.com 检索图像
【发布时间】:2014-06-20 22:46:18
【问题描述】:

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

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

但是什么也没发生,网页没有加载。当我确实喜欢这段代码,但最后没有 url 时,我用 firebug 检查并看到<img src="[object Object]">如何检索图像?谢谢。

编辑

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

【问题讨论】:

  • 一个新的解析用户不会有照片字段。你可能是说 Parse.User.current();
  • 不,我使用collection 检索所有用户并且字符串字段很好。
  • 您提供的代码无法正常工作,您创建了一个空的Parse.User 而不是以正确的方式获得一个(查询或Parse.User.current())并期望它有一个photo属性,然后调用一个方法url(),就好像它是一个属性而不是一个方法......在很多层面上都错了。
  • Parse.User 不为空。有很多笔记。我收到了除photo 之外的所有财产。

标签: javascript collections parse-platform


【解决方案1】:

我不能 100% 确定您要做什么,但是否尝试拉回图像取决于您的应用程序的上下文。

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

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

var profilePhoto = profile.get("photoFile"); $("profileImg")[0].src = profilePhoto.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);
      }
    });

【讨论】:

  • 起初,我没有检索 Content、Person、Anybody else 对象。我想从用户对象中检索,photo 字段(它在那里有文件类型)。这些是用户的图像,但我想只检索一张图像。
【解决方案2】:

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

每当您在字符串中放入某些内容时看到[object Object],这意味着您试图将对象转换为字符串。这里,user.get('photo').url 是一个对象,而不是一个字符串。

【讨论】:

  • :)很好,但我试过了,效果一样(没用)。
  • 哦...你打电话给.url时发生了什么? TypeError 出现了吗? @马克西姆斯
  • 什么都没发生...网页是空的。
  • 嗯...好吧,那祝你好运!希望对这个Parse API有更深入了解的人可以帮到你!
  • 谢谢,但这对我没有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多