salep
salep

Reputation: 1380

Bookshelf.js limit query

Solved. See below for the answer.

I'm trying to get only a limited amount of results.

While the second one works, the first one doesn't. What's missing here?

First query

app.get('/ind', function (req, res) {
  youtube.where('status', 0).fetch(function (qb) {
    qb.limit(10);
  }).then(function (data) {
    data = data.toJSON();
  });
});

Second query

app.get('/photos', function (req, res) {
  user.where('id', 1).fetch({
    withRelated: [{
      'photos': function (qb) {
        qb.limit(2);
      }
    }]
  }).then(function (data) {
    data = data.toJSON();
    res.send(data);
  });
});

Upvotes: 2

Views: 3589

Answers (1)

salep
salep

Reputation: 1380

Solved.

All you need is to add a query() before fetchAll() and define your limit inside this query().

app.get('/ind', function (req, res) {
  youtube.where('status', 0).query(function (qb) {
    qb.limit(10);
  }).fetchAll().then(function (data) {
    data = data.toJSON();
    res.render('youtube', {
      mi: data
    });
  });
});

Upvotes: 4

Related Questions