Firebase离子嵌套搜索

时间:2015-12-17 17:17:00

标签: angularjs ionic firebase angularfire

我试图制作搜索功能。 当用户选择产品时,应显示属于某个存储位置的产品列表。

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

  

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

这是我的代码:

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());
});
};
})

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

这是我的firebase结构:

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>

2 个答案:

答案 0 :(得分: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"]
    }
  }
}

答案 1 :(得分:0)

我通过使用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;
});
 };
})