Muhammad Raihan Muhaimin
Muhammad Raihan Muhaimin

Reputation: 5729

Search query with sails waterline

I am using sailsjs and trying to create a search query based on user's name and/or location. Now I am constructing the query like

   var name = "Harry", 
       location = "Toronto";

   User.findByNameLike( name )
       .exec( function( err, tags ){
            return callbackTag ( null, tags );
       } );

Now where I can add the findByLocationLike tags. I tried

    User.findByNameLike( name )
    User.findByLocationLike( location )
       .exec( function( err, tags ){
            return callbackTag ( null, tags );
       } );

But it gave me an error Object has no method named findByLocationLike. I did

    console.log( User.findByNameLike( name ) );

Interestingly I see a method named findByLocationLike. I am really confused. Anyone has any idea how to join multiple findByLike query in sails waterline.

Upvotes: 1

Views: 4293

Answers (2)

user2805226
user2805226

Reputation:

You can do :

User.findlike({name: name, location: location}).exec(console.log);

this will return all the users with the name harry that live in torronto.

more info:

http://sailsjs.org/#!documentation/models

Upvotes: 1

Muhammad Raihan Muhaimin
Muhammad Raihan Muhaimin

Reputation: 5729

So James' answer would work if I try to join name AND location based on AND condition. However in my case I was using OR clause between those.

So after going through the waterline source code I find the following hack to do what I want.

User.find({ 
           or :[ 
                { like: { name: '%'+name+'%' } }, { like: { location : '%'+location+'%' } } 
               ] 
           } , function ( err, users ){
        // some code here
    });

And this will give me all the user whose name match with either name with name OR location with location.

Upvotes: 4

Related Questions