简体   繁体   中英

Saving query data to a variable for use in later code

How would it be possible to save the below query results into a variable after the query has run?

For example, I want to capture just the first record returned by profileURL: Badgeresults[i].get('SentTo').get('pic'),

I want to save it to a variable so I can then reference it within a div to display on the page.

Can't find a question on SO that answers this directly...

        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'),



                });

            }

Move var Badges = []; out from 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'),



            });

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM