【问题标题】:Restangular - custom search - search within an arrayRestangular - 自定义搜索 - 在数组中搜索
【发布时间】:2014-04-13 13:31:12
【问题描述】:

假设我有一个如下所示的 mongodb items 集合(MongoLab):

{
    "_id": {
        "$oid": "531d8dd2e4b0373ae7e8f505"
    },
     "tags": [
        "node",
        "json"
    ],
    "anotherField": "datahere"
}
{
    "_id": {
        "$oid": "531d8dd2e4b0373ae7e8f505"
    },
     "tags": [
        "ajax",
        "json"
    ],
    "anotherField": "datahere"
  }

我想获取node 在tags 数组中的所有项目。 我尝试了以下方法,但没有成功 - 它正在返回所有项目 - 没有执行搜索?

Plunker 演示:http://plnkr.co/edit/BYj09TOGyCTFKhhBXpIO?p=preview

// $route.current.params.id = "node" - should give me only 1 record with this tag
Restangular.all("items").customGET("", { "tags": $route.current.params.id });

完整示例,两种情况都返回相同的记录:

    var all = db.all('items');

    // GET ALL
    all.getList().then(function(data) {
        $scope.all = data;
        console.log(data);
    });

    // SEARCH for record where "tags" has got "node"
    all.customGET('', { "tags": "node"}).then(function(data) {
        $scope.search = data;
        console.log(data);
    });

任何建议将不胜感激。

【问题讨论】:

  • 您提供了与您的问题无关的代码...那里似乎很好的 Restangular 用法...它应该与您的服务器端有关...您能提供您的后端代码吗?
  • @wickY26 - 我使用 MongoLab 作为后端。所以问题一定出在我的restangular search query。

标签: json angularjs mongodb restangular mlab


【解决方案1】:

根据Mongolab REST API Documentation,您必须使用q 参数传递查询对象。在您的情况下,它是q={"tags":"node"}。

使用 Restangular 会是这样的:

Restangular.all("items").customGET('', { q: {"tags": "node"}})

【讨论】:

猜你喜欢
  • 2019-07-17
  • 1970-01-01
  • 2013-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-13
相关资源
最近更新 更多