JavaScript代码返回所有图像,而不仅仅是为当前用户定义的图像

时间:2014-04-17 14:37:37

标签: javascript jquery parse-platform

以下是JavaScript parse.com代码块。其目的是返回登录到应用程序的当前用户的图像列表。目前代码返回所有图像,而不仅仅是当前用户的图像。

我不确定我错过了什么,除非我还需要在代码块中使用当前用户变量?

  <script type="text/javascript">
    Parse.initialize("xxxxx", "xxxxx");

var currentUser = Parse.User.current();  
var myGlobalBadges = Parse.Object.extend("myBadges");
if (currentUser) {
    var query = new Parse.Query(myGlobalBadges);
    query.exists("BadgeName");
     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('BadgeName'));
        }
     // If the imageURLs array has items in it, set the src of an IMG element to the first URL in the array
        for(var j = 0; j < imageURLs.length; j++){
            $('#imgs').append("<img src='" + imageURLs[j] + "'/>");                 
        }
      },
      error: function(error) {
        // If the query is unsuccessful, report any errors
        alert("Error: " + error.code + " " + error.message);
      }
    });
       } else {


         // show the signup or login page
}   
 </script>

1 个答案:

答案 0 :(得分:0)

我已经设法使用以下代码解决了这个问题。

以下行已从

更改
query.exists("BadgeName");

 query.equalTo("username", currentUser);

  <script type="text/javascript">
    Parse.initialize("xxxx", "xxxx");

var currentUser = Parse.User.current();  
var myGlobalBadges = Parse.Object.extend("myBadges");

    var query = new Parse.Query(myGlobalBadges);
    query.equalTo("username", currentUser);
     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('BadgeName'));
        }
     // If the imageURLs array has items in it, set the src of an IMG element to the first URL in the array
        for(var j = 0; j < imageURLs.length; j++){
            $('#imgs').append("<img src='" + imageURLs[j] + "'/>");                 
        }
      },
      error: function(error) {
        // If the query is unsuccessful, report any errors
        alert("Error: " + error.code + " " + error.message);
      }
    });
         // show the signup or login page

 </script>