Can't find records in Waterline by date/time
Asked Answered
M

2

5

how compare a datetime in sails.js Model? here it is what i did but without 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());
});
Malvasia answered 7/5, 2014 at 19:55 Comment(0)
S
5

The datetime type is transformed into an actual Javascript date in Waterline (the Sails ORM). So it needs to be compared against a date object, not a string. You can keep 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 all Games that start in the future.

Of course since your code is just returning current date, you don't really need moment at all:

var _date = new Date();

will work just as well.

Sailcloth answered 9/5, 2014 at 16:51 Comment(0)
C
1
var _date = moment().format('YYYY-MM-DDTHH:mm:ss.SSS') + 'Z';

you need to convert above string variable to date variable using below line

var tempDate = new Date(_date);

Now use $gte instead of '>='

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());
});

You can also use $gt, $lt, $lte.

Collencollenchyma answered 6/10, 2015 at 13:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.