这个函数从数据库中获取一个问题,并且应该返回它。该数据库是一个 Parse 对象(https://www.parse.com/docs/js_guide)。如代码中的注释所示,该问题可以从 db 调用的成功函数内部访问,但不能从外部访问,并且简单地将 return 语句放在成功块内也不起作用。代码如下。有什么建议么?
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;
}