【问题标题】:AngularFire2 query by child objectAngularFire2按子对象查询
【发布时间】:2017-02-03 21:07:58
【问题描述】:

使用 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!'); }
    });

这是一个例外:

Query: First argument passed to startAt(), endAt(), or equalTo() cannot be an object.

我正在尝试使用此处显示的第二种解决方案,其中过滤条件被推送到包含所有过滤器属性的子对象中: Query based on multiple where clauses in firebase

【问题讨论】:

  • 下面快速戳。如果您共享 JSON 的 sn-p(作为文本,没有屏幕截图),我可以将代码与之匹配。但方法将如下所述。

标签: firebase firebase-realtime-database angularfire2


【解决方案1】:

您不能查询对象。所以这是无效的并导致错误:

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

【讨论】:

  • 我将我的标准放入一个串联的字符串字段中,它就是这样工作的,谢谢
猜你喜欢
  • 1970-01-01
  • 2017-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-14
  • 1970-01-01
  • 1970-01-01
  • 2017-03-11
相关资源
最近更新 更多