0

so I am building a Restful API with nodeJS , Postgres plus sequelize and express as main dependencies.

I'm adding a layer that will pull all orders data from the database upon a /get request on my api. I have 2 main files to get the job done plus a router which I think is irrelevant in this problem so I won't include it :

    // services/orders.js

    import { Order } from '../models'

   const test = () => {
     Order.findAll().then(data => {
         console.log(data)
          })
        };

This seems to works fine since it console out a array that contain the list of items I did input manually into the database. It looks like the following :

[ order {
    dataValues: 
     { id: 0,
       title: 'blue orange',
       date: 2018-11-14T05:20:11.735Z,
       user_id: '8753iuufsd98',
       createdat: 2018-11-14T05:20:16.831Z,
       updatedat: 2018-11-14T05:20:20.072Z },
    _previousDataValues: 
     { id: 0,
       title: 'blue orange',
       date: 2018-11-14T05:20:11.735Z,
       user_id: '8753iuufsd98',
       createdat: 2018-11-14T05:20:16.831Z,
       updatedat: 2018-11-14T05:20:20.072Z },
    _changed: {},
    _modelOptions: 
     { timestamps: true,
       validate: {},
       freezeTableName: false,
       underscored: false,
       underscoredAll: false,
       paranoid: false,
       rejectOnEmpty: false,
       whereCollection: null,
       schema: 'us',
       schemaDelimiter: '',
       defaultScope: {},
       scopes: [],
       indexes: [],
       name: [Object],
       omitNull: false,
       createdAt: 'createdat',
       updatedAt: 'updatedat',
       sequelize: [Object],
       hooks: {},
       uniqueKeys: {} },
    _options: 
     { isNewRecord: false,
       _schema: 'us',
       _schemaDelimiter: '',
       raw: true,
       attributes: [Array] },
    __eagerlyLoadedAssociations: [],
    isNewRecord: false },
  order {
    dataValues: 
     { id: 1,
       title: 'black blue',
       date: 2018-11-14T07:47:09.743Z,
       user_id: 'lksdfjsldjfl',
       createdat: 2018-11-14T07:47:12.698Z,
       updatedat: 2018-11-14T07:47:15.097Z },
    _previousDataValues: 
     { id: 1,
       title: 'black blue',
       date: 2018-11-14T07:47:09.743Z,
       user_id: 'lksdfjsldjfl',
       createdat: 2018-11-14T07:47:12.698Z,
       updatedat: 2018-11-14T07:47:15.097Z },
    _changed: {},
    _modelOptions: 
     { timestamps: true,
       validate: {},
       freezeTableName: false,
       underscored: false,
       underscoredAll: false,
       paranoid: false,
       rejectOnEmpty: false,
       whereCollection: null,
       schema: 'us',
       schemaDelimiter: '',
       defaultScope: {},
       scopes: [],
       indexes: [],
       name: [Object],
       omitNull: false,
       createdAt: 'createdat',
       updatedAt: 'updatedat',
       sequelize: [Object],
       hooks: {},
       uniqueKeys: {} },
    _options: 
     { isNewRecord: false,
       _schema: 'us',
       _schemaDelimiter: '',
       raw: true,
       attributes: [Array] },
    __eagerlyLoadedAssociations: [],
    isNewRecord: false },
  order {
    dataValues: 
     { id: 2,
       title: 'ornage yellow',
       date: 2018-11-14T07:47:31.768Z,
       user_id: 'hfjkseiurr',
       createdat: 2018-11-14T07:47:34.337Z,
       updatedat: 2018-11-14T07:47:36.626Z },
    _previousDataValues: 
     { id: 2,
       title: 'ornage yellow',
       date: 2018-11-14T07:47:31.768Z,
       user_id: 'hfjkseiurr',
       createdat: 2018-11-14T07:47:34.337Z,
       updatedat: 2018-11-14T07:47:36.626Z },
    _changed: {},
    _modelOptions: 
     { timestamps: true,
       validate: {},
       freezeTableName: false,
       underscored: false,
       underscoredAll: false,
       paranoid: false,
       rejectOnEmpty: false,
       whereCollection: null,
       schema: 'us',
       schemaDelimiter: '',
       defaultScope: {},
       scopes: [],
       indexes: [],
       name: [Object],
       omitNull: false,
       createdAt: 'createdat',
       updatedAt: 'updatedat',
       sequelize: [Object],
       hooks: {},
       uniqueKeys: {} },
    _options: 
     { isNewRecord: false,
       _schema: 'us',
       _schemaDelimiter: '',
       raw: true,
       attributes: [Array] },
    __eagerlyLoadedAssociations: [],
    isNewRecord: false } ]

Now , I have a controller with a function getOrders that look like the following and is suppose to return the JSON object outputted by services/orders.js :

// controllers/orders.js
function getOrders(req, res) {
    return res.send({
        data : orderServices.test()
    })
}

The problem here is that upon get request with postman , controllers/orders.js return this :

    //output on postman ...
{}

an empty object .... My question here is how to get the data that have been c-out properly by services/orders.js to controllers/orders.js so express can display it through res.send() and also, why the result of the query don't just stop at :

{ id: 0,
   title: 'blue orange',
   date: 2018-11-14T05:20:11.735Z,
   user_id: '8753iuufsd98',
   createdat: 2018-11-14T05:20:16.831Z,
   updatedat: 2018-11-14T05:20:20.072Z },

instead of having this long array ?

Sorry if my question is long. I tried to make as simple and not confusing as possible.

Cobako
  • 37
  • 8

1 Answers1

0

I can't answer your last question about the format of your data because I'm unfamiliar with Sequelize, but the trouble you're having passing the data to your ExpressJS response stems from a lack of understanding of the Promise pattern. Have your service function return the Promise instead:

// services/orders.js
import { Order } from '../models'
const test = () => Order.findAll();
// export test at some point

And then call the service function inside your controller to access the returned data:

// controllers/orders.js
function getOrders(req, res) {
  orderServices.test()
    .then(data => res.send({ data }));
}

Look into asynchronous programming patterns- callbacks first, then Promises- and the reasoning behind why you must take this approach will become clear.

This answer should cover a considerable amount of the relevant subject matter.

Connor
  • 1,815
  • 3
  • 17
  • 25