Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Waterline (Sails.js). Rows with 'updatedAt' greater than a given value

I want to get the new rows from a model. The rows that have been created after a given date. The query should be very easy:

where updatedAt >= givenDate

But it's not working :(

My code:

        // Date from client
    var lastDate = req.param("date");

    // Parse date with moment? I have tried with and without this.
    lastDate = moment(lastDate).format('YYYY-MM-DD hh:mm:ss');

    var query = Message.find().where({
        updatedAt: {
            '>=':   lastDate
        }
    });

    query.exec(function(err, messages) {
        // I get ALL the messages, :(
        res.send(messages);
    });

Thanks in advance.

like image 854
josec89 Avatar asked Jun 20 '14 21:06

josec89


People also ask

What is sails framework?

The Sails framework is built by a web & mobile shop in Austin, TX, with the help of our contributors. We created Sails in 2012 to assist us on Node.js projects. Naturally we open-sourced it. We hope it makes your life a little bit easier!

How can i Improve my sailsjs experience?

For a better experience on sailsjs.com, update your browser. sails.config.* The Sails framework is built by a web & mobile shop in Austin, TX, with the help of our contributors. We created Sails in 2012 to assist us on Node.js projects.

What is waterline?

Waterline is a new kind of storage and retrieval engine. It provides a uniform API for accessing stuff from different kinds of databases, protocols, and 3rd party APIs.

How do I update a user's name in sails?

To update a particular record, use .updateOne (). await User.update ( { name: 'Pen' }) .set ( { name: 'Finn' }); sails.log ( 'Updated all users named Pen so that their new name is "Finn". I hope they like it.' );


1 Answers

Solved.

I created a Date instance and passed that to the where clause:

// Date from client: Date in MS (new Date().getTime())
    var lastDate = req.param("date");

    // Create date
    lastDate = new Date(lastDate);


    var query = Message.find().where({
        updatedAt: {
            '>=': lastDate
        }
    });

    query.exec(function(err, messages) {
        // I get ALL the messages, :(
        res.send(messages);
    });
like image 59
josec89 Avatar answered Nov 15 '22 01:11

josec89