将查询数据保存到变量以供以后的代码使用

时间:2014-10-23 11:51:43

标签: javascript jquery parse-platform

如何在查询运行后将以下查询结果保存到变量中?

例如,我想只捕获profileURL: Badgeresults[i].get('SentTo').get('pic'),

返回的第一条记录

我想将它保存到变量中,以便我可以在div中引用它以显示在页面上。

无法在SO上找到直接回答此问题的问题......

        query.find({
            success: function(Badgeresults) {
                // If the query is successful, store each image URL in an array of image URL's

                var Badges = [];

                for (var i = 0; i < Badgeresults.length; i++) {

                        Badges.push({

                    imageURL: Badgeresults[i].get('Global_Badges_img'),
                    categorySelected: Badgeresults[i].get('category'),
                    badgeNameSelected: Badgeresults[i].get('BadgeName'),
                    AwardedBy: Badgeresults[i].get('uploadedBy').get('username'),
                    AwardedTo: Badgeresults[i].get('SentTo').get('username'),
                    profileURL: Badgeresults[i].get('SentTo').get('pic'),
                    comment: Badgeresults[i].get('Comment'),



                });

            }

1 个答案:

答案 0 :(得分:1)

var Badges = [];

移出query.find({
var Badges = [];    
query.find({
        success: function(Badgeresults) {
            // If the query is successful, store each image URL in an array of image URL's


            for (var i = 0; i < Badgeresults.length; i++) {

                    Badges.push({

                imageURL: Badgeresults[i].get('Global_Badges_img'),
                categorySelected: Badgeresults[i].get('category'),
                badgeNameSelected: Badgeresults[i].get('BadgeName'),
                AwardedBy: Badgeresults[i].get('uploadedBy').get('username'),
                AwardedTo: Badgeresults[i].get('SentTo').get('username'),
                profileURL: Badgeresults[i].get('SentTo').get('pic'),
                comment: Badgeresults[i].get('Comment'),



            });

        }