使用 AngularFire2 的 Angular 应用程序。我正在尝试查询子对象上的 firebase,而不仅仅是子字符串属性。通过子字符串属性查询可以,但是如何通过整个对象查询。
此代码有效
// Given the following firebase data structure
httpCache: {
{ID}: {
url: 'www.fakeurl',
otherProperties: {}
}
}
// This code will return results if exists where the url matches
this.af.database.list('httpCache', {
query: {
orderByChild: 'url',
equalTo: 'www.fakeurl'
}
}).subscribe(x => {
if (x.length > 0) { console.log('match found!'); }
});
此代码不起作用:
// Given the following firebase data structure
httpCache: {
{ID}: {
request: {
url: 'www.fakeurl',
params: 'id=1'
},
otherProperties: {}
}
}
// This code throws an exception
let request = {
url: 'www.fakeurl',
params: 'id=1'
};
this.af.database.list('httpCache', {
query: {
orderByChild: 'request',
equalTo: request
}
}).subscribe(x => {
if (x.length > 0) { console.log('match found!'); }
});
这是一个异常(exception):
Query: First argument passed to startAt(), endAt(), or equalTo() cannot be an object.
我正在尝试使用此处显示的第二个解决方案,其中过滤条件被推送到包含所有过滤器属性的子对象中: Query based on multiple where clauses in firebase
最佳答案
您无法查询对象。所以这是无效的并导致错误:
let request = {
url: 'www.fakeurl',
params: 'id=1'
};
this.af.database.list('httpCache', {
query: {
orderByChild: 'request',
equalTo: request
}
})
正如我在the question you linked中回答的那样,您可以添加一个包含要查询的组合值的属性。但是,您还需要在实际查询调用中组合这些值,而您并没有这样做。
不可能说出您如何组合这些值,但如果您只是连接 URL 和参数,您将查询为:
let request = 'www.fakeurl_id=1'
this.af.database.list('httpCache', {
query: {
orderByChild: 'request',
equalTo: request
}
})
关于firebase - AngularFire2 通过子对象查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39704249/