Call opt record from javascript

Hi,
I try to call a motoko function from Javascript. On the console I can do that with:

dfx canister call store01 list ‘(opt record {contains=opt “text01”; startsWith=null})’

How can I do that with from the Javascript site: I have tried different variants e.g.:

const a = await store01.list({contains:“”, startsWith:“”});

But I receive the following error:

Error: Invalid opt record {contains:opt text; startsWith:opt text} argument: {“contains”:“”,“startsWith”:“”}

Thanks for some help.

2 Likes

The Javascript binding for optional types is [] | [T] , where an empty array represents null.
This may be what you want to pass in for that example (untested):
[{contains: ["text01"], startsWith: []}]

There’s a reference for the bindings here that can help (look for “Corresponding Javascript values” under each entry):
https://smartcontracts.org/docs/candid-guide/candid-types.html#type-opt

3 Likes

Hi, thank you very much, the JavaScript binding is working now.