Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9170991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:06:09+00:00 2026-06-17T16:06:09+00:00

this function gets a question from a database and is supposed to return it.

  • 0

this function gets a question from a database and is supposed to return it.
The database is a Parse object(https://www.parse.com/docs/js_guide). As indicated in the comments in the code the question is accessible from within the success function of the db call but not from outside it and simply putting the return statement inside the success block doesn’t work either.
Code below. Any suggestions?

function getQuest(){

    var Question = Parse.Object.extend("Question");
    var query = new Parse.Query("Question");
    var questlist = [];
    var newquestion;
    //get list of questions if chosen track is python or java. track is set globally

    if (track == "python")
    {

        query.equalTo("track", "xstsysysus7");

    } else if (track == "java"){

        query.equalTo("track", "XAWqBgxFAP");

    }

    query.find({

        success: function(questions){
        // return list of questions
            var i = Math.floor(Math.random()*10);
            newquestion = questions[i].get('question');
             console.log(newquestion); // works here

    },

        error: function(error){
            console.log(error.message);
        }


    });

    console.log(newquestion); //returns undefined here
    return newquestion;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:06:10+00:00Added an answer on June 17, 2026 at 4:06 pm

    You cannot return from the callback method like this, this is an async issue, you should use a callback method to get your variable out from the method

    function getQuest(callback){
    
    var Question = Parse.Object.extend("Question");
    var query = new Parse.Query("Question");
    var questlist = [];
    var newquestion;
    //get list of questions if chosen track is python or java. track is set globally
    
    if (track == "python")
    {
    
        query.equalTo("track", "xstsysysus7");
    
    } else if (track == "java"){
    
        query.equalTo("track", "XAWqBgxFAP");
    
    }
    
    query.find({
    
        success: function(questions){
        // return list of questions
            var i = Math.floor(Math.random()*10);
            newquestion = questions[i].get('question');
            //call the callback method here and pass your variable as a param
            if(callback != null && callback != undefined){
                  callback(newquestion);
            }
    
    
    },
    
        error: function(error){
            console.log(error.message);
        }
    
    
    });
    

    }

    Now you can call your getQuest method just like this instead of using var newQ = getQuest()

    getQuest(function(newQuestion){
       // do your stuff with newQuestion 
    })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this function in jQuery that gets data from a page with POST,
I have a simple function about neural network. This function gets a matrix, loads
I am trying to call terminate function. [I think this function gets called when
I am working on a code which requires a function. This function gets a
I'm using login function in my site with session. This session of mine gets
This function works fine: std::string get_str() { return std::get<0>( make_tuple(std::string(hi)) ); } But if
This is probably a noob question, but how I've implemented the appendTo() function below
<form id=questions_1> <div data-role=collapsible-set data-theme= data-content-theme= id='q1'> <!-- GETS QUESTIONS FROM THE DATABASE -->
This function is in a Class file in App_Code folder Public Shared Function CRUD(ByVal
This function works as it should, window.setInterval(function(){ var active = $('#frontpageControls a.active'); var next

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.