Cannot find entries in Waterline by date / time

How to compare datetime in sails.js model? this is what i did but no luck.

var _date = moment().format('YYYY-MM-DDTHH:mm:ss.SSS') + 'Z';
Game.find({
    where:{
        active: true,
        start: {
            '>=' : _date
        }
    },
    limit: 1,
    sort: 'start asc'
}, function(err, game) {
    console.log('ERROR: ' + err);
    console.log('Game OBJ' + game.toString());
});
+4
source share
2 answers

The type is datetimeconverted to the actual Javascript date in Waterline (Sails ORM). Therefore, it must be compared with a date object, not a string. You can save your code exactly as it is, but change the first line to:

var _date = new Date(moment().format('YYYY-MM-DDTHH:mm:ss.SSS') + 'Z');

Then your query should find everything Gamethat starts in the future.

Of course, since your code just returns the current date, you really don't need to moment:

var _date = new Date();

will work just as well.

+4
var _date = moment().format('YYYY-MM-DDTHH:mm:ss.SSS') + 'Z';

string string date,

var tempDate = new Date(_date);

$gte ' > ='

Game.find({
    where:{
        active: true,
        start: {
            $gte : tempDate 
        }
    },
    limit: 1,
    sort: 'start asc'
}, function(err, game) {
    console.log('ERROR: ' + err);
    console.log('Game OBJ' + game.toString());
});

$gt, $lt, $lte.

+1

Source: https://habr.com/ru/post/1539644/


All Articles