How to query tailor data on demand

I created a blog using tailor, and I did the search function as described in the tailor video

now what I want is, under the search bar, to show results as the user is typing

to do that I need to be able to query the posts collection on demand and I am not sure how to do that

I tried passing the posts to javascript, to filter the whole posts collection that way, but when trying to parse the encoded string:

const posts = JSON.parse('{{ posts|json_encode|raw }}');

I get ‘unexpected token in json’

Try

const posts = JSON.parse('{{ posts.toJson }}');

posts.toJson returns an empty string unfortunately

That’s probably why the original code doesn’t work either.

Something like this

const posts = JSON.parse('{{ posts ? posts|json_encode : "{}" }}');

nope, I get an unexpected token