2

I'm using Sequelize with node.js and the express framework.

I need to implement a function in my app-module which provides specific information from the database. My current approach is this:

var app = express();

app.provide = function(){
    models.Project.findAll({ include: [{ all: true}]}).then(function(data){
        return data;
    });
};

module.exports = app;

My problem is the query runs asynchronous and the provide function returns an "undefined" before the query finished. If the return statement is not in the callback it works as expected, but there I don't have any data:

var app = express();

app.provide = function(){
    models.Project.findAll({ include: [{ all: true}]}).then(function(data){

    });
    return "test";
};

module.exports = app;

Is it even possible to return data from the database like this?

Thanks in advance!

  • possible duplicate of [How to return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Ben Fortune May 19 '15 at 12:14
  • If I got them right, they suggest to use promises (they use angularJS but I already read about promises in sequelize). But there is no documentation of promises in the sequelize docs http://docs.sequelizejs.com/en/latest/api/promise/. – SaschaThullner May 19 '15 at 12:54
  • That's because promises aren't bound to sequelize, sequelize uses the [Bluebird](https://github.com/petkaantonov/bluebird/blob/master/API.md) library. – Ben Fortune May 19 '15 at 12:56

1 Answers1

1

I could manage to get it work with the following:

app.provide = function(){
    return models.Project.findAll({ include: [{ all: true}]});
};

and then

app.provide().then(function(data){
   console.log(data);
});

wasn't that hard sorry