Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting Data Out #46

Closed
Wemperer opened this issue Jan 2, 2016 · 2 comments
Closed

Getting Data Out #46

Wemperer opened this issue Jan 2, 2016 · 2 comments

Comments

@Wemperer
Copy link

Wemperer commented Jan 2, 2016

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?

var f = require('firenze');
var Database = f.Database;
var MysqlAdapter = require('firenze-adapter-mysql');

var db = new Database({
    adapter: MysqlAdapter,
    host: '127.0.0.1',
    database: 'jlg',
    user: 'root',
    password: ''
});

var Records = db.createCollection({
  table: 'questions',
  alias: 'qs',
});

var questions = new Records();
var STORE_RESULTS_HERE = "";

questions.find()
  .where({
    id: 1
  })
  .first()
  .then(function (questions) {

    // or convert to plain object
    var questionObject = questions.toJSON();
    var answer = questionObject.answer;
   STORE_RESULTS_HERE = answer;
  });

 console.log(STORE_RESULTS_HERE); //returns empty string
@fahad19
Copy link
Owner

fahad19 commented Jan 2, 2016

Welcome to asynchronous programming in JavaScript.

Everything in firenze.js is Promise-based. Bluebird is the library that is used in this case: http://bluebirdjs.com/docs/getting-started.html

Promises can be chained (in a flat way, avoiding nested callbacks).

Your above query can be written as:

var promise = questions.find()
  .where({...})
  .first()
  .then(function (question) {
    var questionObject = question.toJSON();
    var answer = questionObject.answer;

    return answer; // let's chain it now
  })
  .then(function (answer) {
    // returned value from previous .then() becomes the `answer` here
    console.log(answer);

    return answer; // 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.

More docs on promises here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise

Hope that helps!

@fahad19 fahad19 closed this as completed Jan 2, 2016
@fahad19
Copy link
Owner

fahad19 commented Jan 2, 2016

@Wemperer: the guides here should help you get started http://firenze.js.org/docs/guides/index.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants