【问题标题】:Firebase Ionic Nested SearchFirebase 离子嵌套搜索
【发布时间】:2015-12-23 23:15:19
【问题描述】:

我正在尝试创建搜索功能。 当用户选择产品时,应显示属于某个存储位置的产品列表。

到目前为止,我只能显示包含所有项目的数组 Products。但是当我想更深入并遍历每个项目(以搜索与所选项目匹配的项目)时,我得到了一个错误:

错误:没有为产品定义索引

这是我的代码:

controllers.js:

.controller('InventoryCtrl', function($scope, Category, Inventory) {
$scope.categories = Category;


$scope.searchInventory = function(category){
var word = category.Category;
//console.log(word);


var ref = new Firebase('https://vivid-heat-2430.firebaseio.com/');
var invenRef = ref.child('Inventories');
var prodRef = invenRef.child('Products');


invenRef.orderByChild("Products").on("child_added", function(snapshot) {
  var data = snapshot.val();
  var store = data.Inventory;
  var prod = data.Products;
  console.log(prod);


  snapshot.forEach(function(childSnapshot) {
    var test = childSnapshot.val();
    console.log(test);
    //var key = childSnapshot.key();
    //console.log(key);
  });

});
};
})

我已经定义了我的索引。但我仍然得到这个错误。

我也在我的控制器中这样尝试过:

$scope.searchInventory = function(category){
var word = category.Category;

var ref = new Firebase('https://vivid-heat-2430.firebaseio.com/');
var invenRef = ref.child('Inventories');
var prodRef = invenRef.child('Products');

invenRef.orderByChild("Products").equalTo(word).on("child_added", function(snapshot){
  console.log(snapshot.key());
});
};
})

这里我没有错误,但我无法记录任何内容。

这是我的火力基地结构:

Database structure

你能指出我做错了什么吗?我对此仍然很陌生,并试图从我的错误中吸取教训。

提前致谢!

编辑

我的规则.json

{
"rules": {
    ".read": true,
    ".write": true,
    "Inventories": {
    ".indexOn": "Products"
    }
}
}

db结构的JSON文件:

{
  "Categorie" : {
    "-K5a3iGlgi7PS0m3O4y8" : {
      "Category" : "IQ 33cl BOX",
      "Optional" : false,
      "Size" : "24"
    },
    "-K5a3vNZRc2Cur9964Xs" : {
      "Category" : "CL 33cl BOX",
      "Optional" : true,
      "Size" : "24"
    },
    "-K5a40Q79SCqWqMbqWQu" : {
      "Category" : "IQ 33cl CASE",
      "Optional" : true,
      "Size" : "24"
    },
    "-K5a464FON4qdnqE9rgf" : {
      "Category" : "CL 33cl CASE",
      "Optional" : false,
      "Size" : "24"
    },
    "-K5a4TAzHE8cRGPbNeij" : {
      "Category" : "Empty CASES",
      "Optional" : false,
      "Size" : "24"
    }
  },
  "Inventories" : {
    "17-12-2015Storage 1" : {
      "Date" : 1450366396979,
      "Inventory" : "Storage 1",
      "Products" : {
        "CL 33cl BOX" : {
          "boxes" : 0,
          "full" : 11,
          "half" : 13
        },
        "IQ 33cl BOX" : {
          "boxes" : 0,
          "full" : 60,
          "half" : 0
        }
      }
    },
    "17-12-2015Storage Deb" : {
      "Date" : 1450367128198,
      "Inventory" : "Storage Deb",
      "Products" : {
        "IQ 33cl CASE" : {
          "boxes" : 0,
          "full" : 2,
          "half" : 14
        }
      }
    }
  },
  "Storages" : {
    "-K5a4esu-1Na1hkKMP47" : { "name" : "Storage 1" },
    "-K5a4ihb9L5z6qSqQxAx" : { "name" : "Storage Deb" },
    "-K5a4l9odWuPUJJuN8OR" : { "name" : "Storage Bart" },
    "-K5a4nsc47N3k_hMVl2h" : { "name" : "Storage Debosz" }
  }
}

index.html

<div style="max-height: 300px" ng-controller="InventoryCtrl">
      <ion-list>
        <ion-radio ng-repeat="category in categories" class="item-accordion" ng-model="checked.check" value="{{category.Category}}" ng-click="searchInventory(category)">
          <p>{{category.Category}}</p>
        </ion-radio>
        <br>
      </ion-list>

    </div>

【问题讨论】:

  • 你能说明你是如何在你的 rules.json 中定义索引的吗?
  • @FrankvanPuffelen 我刚刚用 rules.json 编辑了我的帖子
  • 好的,谢谢。这让我们更接近。现在,不是链接到 JSON 结构的图像,而是可以将该 JSON 转换为文本格式并将其包含在 问题中吗?这也将解决图像未显示更深层结构的问题,这可能与问题有关。获取 JSON 的一种快速方法是从 Firebase 仪表板中导出它。
  • @FrankvanPuffelen 我在问题中添加了 json
  • 格式有点让我失望,但是 Products 是“17-12-2015Storage 1”的子代,它是 Inventories 的子代吗?

标签: angularjs ionic firebase angularfire


【解决方案1】:

你的结构(根据链接)

Inventories
  some_node_name_1
    Inventory: xxxx
    Products:
      product_name: "product a"
      product_price: 30
  some_node_name_2
    Inventory: xxxx
    Products:
      product_name: "product b"
      product_price: 50

以及您的查询路径

var ref = new Firebase('https://vivid-heat-2430.firebaseio.com/');
var invenRef = ref.child('Inventories');
var prodRef = invenRef.child('Products');

(注意 prodRef 似乎没有被使用)

那么怎么样:

var ref = new Firebase('https://vivid-heat-2430.firebaseio.com/Inventories');
  ref.orderByChild("Products/product_name").on("child_added", function(snapshot) {
});

并且 .indexOn 规则应该与您执行查询的位置 (product_name) 处于同一级别。

编辑更多信息:

节点键会自动编入索引,因此您无需为节点产品添加 .index(它已经完成)。但是,您可以为 Products 的子级添加索引,例如:

{
  "rules": {
    "Inventories": {
        "Products": {
           ".indexOn": ["product_name"]
        }
    }
  }
}

或者(我相信这是另一种选择)

{
  "rules": {
    "Inventories": {
       ".indexOn": ["Products/product_name"]
    }
  }
}

【讨论】:

  • 也许我在问我的问题时不清楚。在产品中有不同的项目。搜索商品时,我需要在产品中进行查询以匹配商品。例如,如果我选择了 yyyyy 进行搜索,那么我需要在产品中查找 yyyyy。我不需要深入了解 yyyyy。所以如果我是正确的,我的 .indexOn 规则仍然正确吗?
  • 这让我很困扰 orderByChild("Products")。由于 Products 是一个节点名称并且没有与之关联的直接值,因此预期的结果是什么?请参阅上面我编辑的答案
【解决方案2】:

我已经通过使用 Object.keys() 来获取产品名称解决了这个问题。

我还使用了一个 for 循环来搜索匹配的产品。

我的控制器现在看起来像这样:

.controller('InventoryCtrl', function($scope, $firebaseObject, Category, Inventory) {

$scope.categories = Category;

$scope.searchInventory = function(category){
var word = category.Category;

var ref = new Firebase('https://vivid-heat-2430.firebaseio.com/');

var invenRef = ref.child('Inventories');
var prodRef = invenRef.child('Products');

var test22 = [];

invenRef.orderByChild("Products").on("child_added", function(snapshot) {
  var data = snapshot.val();
  var store = data.Inventory;
  var test = Object.keys(prod);

  for( var i = 0; i < test.length; i++){
    if (test[i] == word) {        
     test22.push(data.Inventory);
    };        
  }; 
  $scope.show = test22;
});
 };
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-28
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-17
    • 2011-09-14
    相关资源
    最近更新 更多