Parse JavaScript ガイドから:
ファイルの内容を取得する最適な方法は、アプリケーションのコンテキストによって異なります。クロスドメイン リクエストの問題があるため、ブラウザに作業を任せることができるのが最善です。通常、これはファイルの URL を DOM にレンダリングすることを意味します。ここでは、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);
}
});