1

我正在使用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 查询。

4

2 回答 2

4

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

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

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

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

于 2014-04-10T18:26:15.520 回答
1

你可以做 :

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

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

更多信息:

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

于 2014-04-10T09:08:56.240 回答