0

I am new to express js and sequelize. I want to display all users and their orders(can be more than 1).

My code is

app.get('/test', function (req, res) {
  var arr = [];
  db.user.findAll().then(
    function (resp) {
      for (i = 0; i < resp.length; i++) {

        arr.push(Object.assign({},
          {
            user_id: resp[i]['id'],
            user_email: resp[i]['email'],
            data: x(resp[i]['id'])
          }))
      }
      res.send(arr);
    }
  );
});

and the function that will give me orders of particular user

var x = function (n) {
  var arr1 = [];
  db.order.findAll({ where: { userId: n } }).then(function (response) {

    for (i = 0; i < response.length; i++) {
      arr1.push(Object.assign({},
        {
          order_id: response[i]['id'],
          order_total: response[i]['totalAmount']
        }));
    }
  });
  return arr1;
}

This is only showing me all users but not their particular orders, the array length returned by function 'x' is 0, I know this problem is related to promises and resolved results. I have tried .resolve() , .success() but nothing works . How can I fix it ? Thanks

Daniyal Javaid
  • 1,426
  • 2
  • 18
  • 32
  • 1
    `x` **also** has to return a promise, as `findAll` is an asynchronous operation. There's also no need for that `Object.assign` call. So `var x = function() { return db.order.findAll({ where: { userId: n } }).then(function(response) { return response.map(function(entry) { return { order_id: response[i]['id'], order_total: response[i]['totalAmount'] }; }); }); };` That returns the promise created by calling `then`, which will resolve when the `findAll` promise resolves, using the result of mapping the `response` array as its resolution value. – T.J. Crowder Apr 17 '17 at 10:50
  • 1
    after changing the code now I am getting this : "data": { "isFulfilled": false, "isRejected": false } – Daniyal Javaid Apr 17 '17 at 10:54
  • 1
    and this error : Unhandled rejection TypeError: Cannot read property 'id' of undefined, because i think no promise was resolved – Daniyal Javaid Apr 17 '17 at 10:54
  • I just forgot to replace `response[i]` with `entry`, it should be: `var x = function() { return db.order.findAll({ where: { userId: n } }).then(function(response) { return response.map(function(entry) { return { order_id: entry.id, order_total: entry.totalAmount }; }); }); };` The promise creaetd by `then` was getting rejected because accessing `response[i]` threw an error (understandably). – T.J. Crowder Apr 17 '17 at 10:58
  • Re "Unhandled rejection," remember that a promise may be resolved *or* rejected. So the code *using* `x` will want to handle both resolutions and rejections (unless that code is just returning a promise chained to the one returned by `x`, in which case the code calling *it* would handle rejections, etc.). – T.J. Crowder Apr 17 '17 at 11:00
  • changed the code but still I get this result : { "user_id": 1, "user_email": "dj@gma", "data": { "isFulfilled": false, "isRejected": false } } – Daniyal Javaid Apr 17 '17 at 11:04
  • on console I can see the 'order' query executed but why i cant get it on response :/ , spent more than 6 hours to fix this but nothing is working – Daniyal Javaid Apr 17 '17 at 11:09

0 Answers0