2017-03-05 67 views
0

我使用的Bookshelf.js ORM,我需要能够按日期和有一些问题进行查询。如何查询日期Bookshelf.js

这里是我使用的一个样本代码片段。

 this.model.query((q) => { 
      q.where('created_at', '>=', Date.now()); 
      q.orderBy('created_at', 'DESC'); 
     }) 
     .fetch().then(function (model) { 
      socket.send(JSON.stringify(model)); 
     }); 

上面的代码只是返回一个空或空的数组。 任何想法?

回答

0

真的很遗憾你的查询返回空结果,查询未来是一个非常漂亮的功能。

但我认为你想在过去创建的数据,对不对?因此,尝试

this.model.query((q) => { 
    q.where('created_at', '<=', new Date()); 
    q.orderBy('created_at', 'DESC'); 
}) 
    .fetch().then(function (model) { 
    socket.send(JSON.stringify(model)); 
    }); 

还要注意Date.now(),使该milliseconds elapsed since the UNIX epochnew Date()给出正确的当前日期/时间替换。

无论如何,标准没有多大意义,因为它基本上意味着所有行。当然,除非你正在关注created_at列上的某个bug或者过滤出NULL属性。