https://discord.gg/botpress logo
Find Records Error using var as param
# 🤝help
e
Hi I encountered a strange error when trying to use the Find Records Card. My goal is to retrieve information from a database, but different queries need to be applied based on the situation. When I use the Find Records Card and directly input a query like "Price is under 250000", I get the database results perfectly. However, if I replace the query with a string variable that contains the same text, such as {{workflow.dynamicQuery}} which contains "Price is under 250000", I get an error. Here's what appears in the logs for the first example: Execute operation findRecords({"filter": {"type": "AIQuery", "template":"price is under 300000", "params": [], "types": []}}) For the second example, it looks slightly different: Execute operation findRecords({"filter":{"type":"AIQuery","template":"{params[0]}","params":["Price is under 300000"],"types":["string"]}}) In the second example (the one with the variable), the template is "template":"{params[0]}", whereas in the first example, the string is directly in the template as "template":"price is under 300000". As a result, the second example tries to interpret "template":"{params[0]}" with params:["Price is under 300000"], which gives the first letter as the command, in this case, "P". This leads to the following error: Could not execute AI query: SyntaxError: Unexpected token 'P', "Price is u"... is not valid JSON Why can I pass a string directly as a parameter, but not a variable of type String? Do you have any ideas or solutions for this issue? https://cdn.discordapp.com/attachments/1261019677879177249/1261019678386557008/Screenshot_2024-07-11_at_2.01.12_PM.png?ex=66916f97&is=66901e17&hm=6a518ff2ea2507a3138f86410f9299df1be08233a527a7530642813bdcfc4976& https://cdn.discordapp.com/attachments/1261019677879177249/1261019678663245915/Screenshot_2024-07-11_at_12.24.21_PM.png?ex=66916f98&is=66901e18&hm=dd527754f3f471392f5c38e5397e20eb25b1601399d41bbccab0ee867edb0181&
r
@early-shoe-82728 Unfortunately the Find Records card doesn't support entire templates, only placeholders. For example
price is under {{workflow.price}}
, which behind the scene will translate the prompt using GPT to a generator function that looks like
Copy code
AI Query generated successfully
{ query: { Price: { '$lt': 200 } },
  generator: 'function (util, ...params) {
    return {
        // "price is under {params[0]}"
        Price: { $lt: Number(params[0]) }
    };
}' }
The reasoning behind this feature is to generate and cache the function, so that GPT isn't called every time you want to query the table with the same parameter types. In your case, it's not possible to generate a generator function if the entire query is variable. There's an alternative coming in about 2 weeks for this, but for now the only workaround is to use an execute code card and do something like this:
Copy code
workflow.rows = await Data2Table.findRecords({
  filter: AI.apply(this, [workflow.dynamicQuery] as any)
})
This forces the generation of a generator function every time.
e
Tnx for the explanation. I'll try the Code Card. When the update will be done, you can share the alternative here 🙂 I would be happy to learn the new way.