我正在尝试过滤从FindAll返回的数据,因为我只想要来自某个提供商的数据
// Data coming back from API
{
"-KDinaItb7lkHpai-DlG": {
"email": "johns@test.com",
"name": "John Smith",
"notes": "John is a great employee and is the best",
"phone": "215-543-9830",
"provider": "-KDhzbilOvv7Evuc5S_X"
},
"-KDjS0cCxFWQctcwXg0V": {
"email": "amanda@test.com",
"name": "Amanda Harrington",
"notes": "Amanda is a great employee",
"phone": "215-543-9830",
"provider": "-KDiokWebdhTNKTORWwn"
},
"-KDyf7pU_PyxRQSgFB59": {
"email": "lguy@test.com",
"name": "Larry Guy",
"notes": "He is a funny guy",
"phone": "702-454-2397",
"provider": "-KDhzbilOvv7Evuc5S_X"
}
}
// In the route
let providerId = model.get('provider').get('id');
this.store.findAll('employee').then(function(results) {
let prov = results.filterBy('provider', providerId);
console.log(prov);
});
当控制台日志发生时,它返回一个空数组。我认为它是因为ID而不是看嵌套对象。有人有任何想法吗?
答案 0 :(得分:1)
好的,你的哈希看起来很奇怪。属性名称不应该是一些生成的哈希值。
代码应该是这样的。 我假设你在数组中的索引0上有1个包装器对象。
var filteredEmployees_promise = this.store.findAll('employee').then(function(results) {
var filteredResults = [];
Object.keys(Results[0]).forEach(key => {
var filteredObj = Results[0][key][providerId];
if(Ember.isPresent(filteredObj) {
filteredResults.pushObject(filteredObj)
}
});
return filteredResults;
});
以后
filterEmployees_promise.then(employees => { // Custom stuff })