2017-03-05 45 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屬性。