Between Dates using Waterline ORM SailsJS

我只是一个虾纸丫 提交于 2019-12-12 08:54:48

问题


Goal: Return a list of items that were created between two dates.

According to this issue https://github.com/balderdashy/waterline/issues/110 there is no between function just yet. However the work around is the following:

User.find({
    date: { '>': new Date('2/4/2014'), '<': new Date('2/7/2014') }
}).exec(/* ... */);

To be more exact, we don't want the hard coded dates above so we read in the input from a form submission like so:

    start = new Date(req.param('yearStart') + '/' + req.param('monthStart') + '/' + req.param('dayStart'));
    end = new Date(req.param('yearEnd') + '/' + req.param('monthEnd') + '/' + req.param('dayEnd'));

Printing start and end to console shows me this (different timezones for some reason)?

from: Sat Mar 01 2014 00:00:00 GMT-0500 (EST)
to: Sat Apr 30 2016 00:00:00 GMT-0400 (EDT)

However my view returns nothing every time.


回答1:


While writing this question I realized the issue was that I had date instead of createdAt in my filter.

So the following works:

User.find({
    createdAt: { '>': start, '<': end }
}).exec(/* ... */);



回答2:


If you are wondering how to use the API blueprint query, you will need to use the toISOString() method of the Date object. For example : http://localhost:1337/:model/?where={date: {'<=', date.toISOString()}}



来源:https://stackoverflow.com/questions/29334400/between-dates-using-waterline-orm-sailsjs

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