$in Clause in Advanced Query Bad Validation Error

The query being used.

{
  "$and": [
    {
      "scheduleId": {
        "$in": "{{data.query.id}}",
        "$ci": true
      }
    }
  ]
}

Returns a validation error at the output location -
"message": "scheduleId: value for $in must be an array"

A simple mutate that pushes the type ({{typeof data.query.id}}) outputs array.

A thing cannot be an array and not an array at the same time.

Hi @Travis_Rhoden, and welcome to the Losant Forums.

The default Handlebars behavior when using an expression to evaluate an array is similar to the JavaScript Array.prototype.toString() method. I couldn’t find this in their documentation but you can reproduce it on their website:

https://handlebarsjs.com/examples/literal-segments.html

So in your case, data.query.id may be an array but rendering it as you are in this template will output it as a comma-separated string - and if it did output as an array, it would still be treated as a string in the request because of the quotes "" you wrapped the value with.

What you want to use instead is Losant’s {{jsonEncode}} helper like so:

{
  "$and": [
    {
      "scheduleId": {
        "$in": {{jsonEncode data.query.id}},
        "$ci": true
      }
    }
  ]
}

I encourage you to test your templates out in our built-in workflow template tester if you run into these kinds of unexpected issues in the future.

Hopefully this helps. Let us know if you have any other questions.

1 Like