用sails水线搜索查询

时间:2014-04-09 23:35:22

标签: sails.js waterline

我正在使用sailsjs并尝试根据用户的名称和/或位置创建搜索查询。现在我正在构建像

这样的查询
   var name = "Harry", 
       location = "Toronto";

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

现在我可以添加findByLocationLike标签。我试过了

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

但它给了我一个错误对象没有名为findByLocationLike的方法。我做了

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

有趣的是,我看到一个名为findByLocationLike的方法。我真的很困惑。任何人都知道如何在sails水线中加入多个findByLike查询。

2 个答案:

答案 0 :(得分:3)

如果我尝试根据AND条件加入名称和位置,那么詹姆斯的回答是有效的。但是在我的情况下,我在那些之间使用了OR子句。

所以在浏览了水线源代码后,我发现以下黑客可以做我想做的事情。

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

这将为我提供名称与姓名或位置匹配的名称匹配的所有用户。

答案 1 :(得分:1)

你可以这样做:

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

这将返回所有名为harry的用户,这些用户位于torronto。

更多信息:

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

相关问题