使用javascript从Parse.com检索图像
我需要你帮我回答这个问题。我试着这样做:使用javascript从Parse.com检索图像,javascript,collections,parse-platform,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 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检查并看到
如何检索图像?多谢各位
编辑
当我使用对象.get('photo').url时,我得到未定义的我不知道实际是什么,但我做了一些快速的研究,我想你必须调用.url()
方法:
当您将某个对象放入字符串中时,无论何时看到[object object]
,这意味着您试图将某个对象转换为字符串。在这里,user.get('photo')。url
是一个对象,而不是字符串。我不能100%确定您要做什么,但要想把图像拉回来,取决于应用程序的上下文
这是针对单个图像还是多个图像
您可以使用jQuery在页面上呈现上传的个人资料照片:
var profilePhoto=profile.get(“photoFile”);
$(“profileImg”)[0]。src=profilePhoto.url()
-查询包含所需图片的行的内容类
-获取图片的URL
-设置图像元素的源
更多信息请点击这里
示例代码:
// 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);
}
});
//查询内容类中存在图像的行
var Content=Parse.Object.extend(“Content”);
var query=newparse.query(内容);
query.exists(“图像”);
查询.查找({
成功:功能(结果){
//如果查询成功,请将每个图像URL存储在图像URL的
ImageURL=[];
对于(var i=0;i0){
$('#color').attr('src',imageURL[0]);
}
},
错误:函数(错误){
//如果查询不成功,请报告任何错误
警报(“错误:+Error.code+”“+Error.message”);
}
});
新的解析用户不会有照片字段。您可能是指Parse.User.current();不,我使用集合
检索所有用户,字符串字段可以正常工作。您提供的代码无法工作,您创建了一个空的解析。用户
,而不是以正确的方式获取一个(查询或Parse.User.current()
),并希望它具有photo
属性,然后调用一个方法url()
好像它是一个属性而不是一个方法。。。在这么多级别上都是错误的。Parse.User
不是空的。有很多注释。我收到了所有的属性,除了照片:)很好,但是我尝试了这个,效果一样(没有效果)。哦…当你调用.url
时发生了什么?是否出现了TypeError
@没有发生什么事…网页是空的。嗯…好吧,祝你好运!我希望对这个Parse
API有更好理解的人能帮助您!Thanx,但这对我没有帮助。首先,我不检索内容、人物或任何其他对象。我想从用户对象中检索,photo
字段(它有文件类型)。这些是为用户的图像,但我想检索甚至只有一个图像。