Select object created between two dates by Bookshelf, MySQL, Knex on node.js

喜你入骨 提交于 2019-12-08 03:29:55

问题


I need to get a list of an object created between two times;

I am able to get data by writing SQL Query;

But I need to write this query by Bookshelf;

My simple query:

    router.route('/locations')
  // fetch all locations
  .get(function (req, res) {
    Locations.forge()
    .fetch()
    .then(function (collection) {
      res.json({error: false, data: collection.toJSON()});
    })
    .catch(function (err) {
      res.status(500).json({error: true, data: {message: err.message}});
    });
  })

how can I get location between two dates like above query?


回答1:


Try adding a query() call to the chain, like

Locations
  .forge()
  .query(function(qb) {
    qb.whereBetween('creation', [initialDate, finalDate]);
  })
  .fetch()
  .then(function (collection) {
    res.json({error: false, data: collection.toJSON()});
  })
  .catch(function (err) {
    res.status(500).json({error: true, data: {message: err.message}});
  });

See Bookshelf Collection.query() and Knex Query Builder and whereBetween()



来源:https://stackoverflow.com/questions/41417730/select-object-created-between-two-dates-by-bookshelf-mysql-knex-on-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!