WJS
WJS

Reputation: 21

how to use datetime in sails model

i would like to have a function in my controller to get data based on time range. First, i have all the data in mongodb, there is a attribute ModifiedTime as string looks like 2015-02-25T17:17:33Z. Second, i define the model in sails with ModifiedTime:

{ type: 'datetime', columnName: 'ModifiedTime' }

In the model.js, I set schema: true. Then in my controller, i try to use

 User.find({ModifiedTime : {'<=' : new Date('2015-03-18T00:00:00Z')}}).exec(function(err,st){
      if (err) return res(err);
          if (!st) return res(new Error('Invalid ModifiedTime.'));

          return res.json(st);
    } );

But i get nothing, see always [] in the browse. I used waterline http://localhost:1337/User to check the data in browse. i can see all the data from mongodb. The strange thing is, i see something like ModifiedTime": "2015-02-18T17:36:53Z. so, for me, it looks like the ModifiedTime in sails is still a string, am i right? but i set the type as datetime in the model. I hope, it could transfer the string of mongodb to datetime in background, won't it? Please give some advise. i spend already too much time for that :( thank you very much! WJS

Upvotes: 2

Views: 4394

Answers (1)

Meeker
Meeker

Reputation: 5979

You're right / wrong.

In your DB you state the datetime is a string. If it is already a string then you can't set it to be a date in your sails model. You should simply compare two strings instead of transforming 2015-03-18T00:00:00Z into a date.

 User.find({ModifiedTime : {'<=' : '2015-03-18T00:00:00Z'}}).exec(function(err,st){
      if (err) return res(err);
          if (!st) return res(new Error('Invalid ModifiedTime.'));

          return res.json(st);
    } );

If you truley want to use Date/Time then you must go through your original data and change modified time to a date/time object.

Upvotes: 0

Related Questions