You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm fairly new to this and I've figured out how to use firenze to query a database and get results but I cannot get my results to the global scope to use elsewhere in my code. How do you get the results out?
varf=require('firenze');varDatabase=f.Database;varMysqlAdapter=require('firenze-adapter-mysql');vardb=newDatabase({adapter: MysqlAdapter,host: '127.0.0.1',database: 'jlg',user: 'root',password: ''});varRecords=db.createCollection({table: 'questions',alias: 'qs',});varquestions=newRecords();varSTORE_RESULTS_HERE="";questions.find().where({id: 1}).first().then(function(questions){// or convert to plain objectvarquestionObject=questions.toJSON();varanswer=questionObject.answer;STORE_RESULTS_HERE=answer;});console.log(STORE_RESULTS_HERE);//returns empty string
The text was updated successfully, but these errors were encountered:
Promises can be chained (in a flat way, avoiding nested callbacks).
Your above query can be written as:
varpromise=questions.find().where({...}).first().then(function(question){varquestionObject=question.toJSON();varanswer=questionObject.answer;returnanswer;// let's chain it now}).then(function(answer){// returned value from previous .then() becomes the `answer` hereconsole.log(answer);returnanswer;// if you want to chain further later});// we can still continue here again...promise.then(function(answer){console.log(answer);});
I don't see why you would want to expose the answer to a global variable though. That's not how Promises are intended to be used.
I'm fairly new to this and I've figured out how to use firenze to query a database and get results but I cannot get my results to the global scope to use elsewhere in my code. How do you get the results out?
The text was updated successfully, but these errors were encountered: