【发布时间】: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