【问题标题】:Search query with sails waterline带帆水线的搜索查询
【发布时间】:2014-04-09 23:35:21
【问题描述】:

我正在使用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 的方法。我真的很困惑。任何人都知道如何在风帆水线中加入多个 findByLike 查询。

【问题讨论】:

    标签: sails.js waterline


    【解决方案1】:

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

    因此,在浏览完水线源代码后,我发现以下 hack 可以做我想做的事。

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

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

    【讨论】:

      【解决方案2】:

      你可以这样做:

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

      这将返回所有居住在 torronto 的名为 harry 的用户。

      更多信息:

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

      【讨论】:

      • 这不是我想要的。我想使用 Like 进行搜索。因为当用户搜索时,我们需要提供匹配的内容。
      • 我认为您将我的回答“find”改为“findlike”
      • 实际上,如果我想根据 AND 子句加入名称和位置,你的就可以了,但在我的情况下,我想使用 OR 加入它们。然而,我的问题没有表达是我的错,所以我会接受你的回答。请在您的回答中编辑 User.find -> User.findlike;我会接受的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-12
      • 2014-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多