All questions

Can I pass a value into croct.evaluate instead of hardcoding it in the query?

Asked by sam.codes on

Ssam.codes

I am calling evaluate from client code and I want to reuse one query across a few thresholds without string-concatenating numbers into the expression:

croct.evaluate("user's score is greater than 90")

Is there a supported way to inject the threshold at call time instead of baking it into the string

Was this helpful?

2 answers

DODevin Ortiz

Yes, pass them through the attributes option and reference them by name in the query:

croct.evaluate("user's score is greater than threshold", {  attributes: {threshold: 90}})

Now the same query string covers every threshold, you just change the value you pass. Inside CQL you can also declare values with let if you want to name intermediate things for readability, but for injecting from JS the attributes option is the clean path.

Was this helpful?
Ppriy_dev

Two things I would add from running these in production. Always tail it with a fallback so a failed evaluation degrades gracefully instead of throwing:

croct.evaluate("user's score is greater than threshold", {  attributes: {threshold: 90}}).catch(() => false)

And keep the query compact. There is a size limit and an oversized expression comes back as a "Too complex query" error, the too complex query thread covers what to trim. If you just want to poke at expressions while developing, the test an audience in the browser console approach is handy.

Was this helpful?
Ssam.codes

The catch-to-false is a good shout, I was letting it reject. Swapped all our calls over.

Still have questions?