Realm-js: how to filter a string with a variable

Created on 23 Jun 2016  路  3Comments  路  Source: realm/realm-js

I am trying to filter the realm ListView with a text input. I need to search by the subdivisionName property. dynamically filter the list while typing. I cant seem to figure out how to use a variable with the filter predicate.

filter function

    _nameFilter(val) {
        this.setState({ dataSource: ds });
        let result = this.state.subdivisions.filtered('subdivisionName CONTAINS[c]' + this.state.nameFilter);
        this.setState({ dataSource: this.state.dataSource.cloneWithRows(result) })
    }

I am getting

subdivisionName CONTAINS[c]:1:0: Invalid predicate

Most helpful comment

The space after CONTAINS[c] is nice to have, but not _necessary_, but what is necessary is to either quote your string or better yet, use an argument placeholder like this:

this.state.subdivisions.filtered('subdivisionName CONTAINS[c] $0', this.state.nameFilter);

All 3 comments

Looks like you are missing an argument after CONTAINS (if this.state.nameFilter is null or an empty string). You will also need a space after contains in your query.

The space after CONTAINS[c] is nice to have, but not _necessary_, but what is necessary is to either quote your string or better yet, use an argument placeholder like this:

this.state.subdivisions.filtered('subdivisionName CONTAINS[c] $0', this.state.nameFilter);

great. that did the trick! maybe you could put a couple of these examples in the react-native docs?

Was this page helpful?
0 / 5 - 0 ratings