0

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



                });

            }

1 Answer 1

1

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



            });

        }
Sign up to request clarification or add additional context in comments.

2 Comments

A better solution is use a callback function, because query.find is asyncronous and pass to it the variable Badges:
can you show me an example of a call back function?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.