# How to call and declare a method that needs to receive a vector of number and text values

**URL:** <https://forum.dfinity.org/t/how-to-call-and-declare-a-method-that-needs-to-receive-a-vector-of-number-and-text-values/23858>\
**Category:** Getting Started\
**Created:** [October 18, 2023, 9:47am UTC](https://forum.dfinity.org/t/how-to-call-and-declare-a-method-that-needs-to-receive-a-vector-of-number-and-text-values/23858 "2023-10-18T09:47:00Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![ildefons](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ildefons/32/2638_2.png) [@ildefons](https://forum.dfinity.org/u/ildefons)\
**Post date:** [October 18, 2023, 9:47am UTC](https://forum.dfinity.org/t/how-to-call-and-declare-a-method-that-needs-to-receive-a-vector-of-number-and-text-values/23858/1 "2023-10-18T09:47:00Z")

</div>

I would like to declare a canister method that is able to receive a vector with an arbitrary number of Text and Nat values. For instance [1;“y”;3; “x”] or [1;2;3,“z”]. How should I update the following method declaration?  
`setData: (vec XX) -> ();`

---

<div class="post-metadata">

**Author:** ![sea-snake](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/sea-snake/32/7243_2.png) [@sea-snake](https://forum.dfinity.org/u/sea-snake)\
**Post date:** [October 18, 2023, 10:26am UTC](https://forum.dfinity.org/t/how-to-call-and-declare-a-method-that-needs-to-receive-a-vector-of-number-and-text-values/23858/2 "2023-10-18T10:26:11Z")

</div>

Have a look at the `variant` type, each vector entry would be basically a variant of either nat or text.

---

<div class="post-metadata">

**Author:** ![sea-snake](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/sea-snake/32/7243_2.png) [@sea-snake](https://forum.dfinity.org/u/sea-snake)\
**Post date:** [October 18, 2023, 10:30am UTC](https://forum.dfinity.org/t/how-to-call-and-declare-a-method-that-needs-to-receive-a-vector-of-number-and-text-values/23858/3 "2023-10-18T10:30:29Z")

</div>

Just to clarify, candid is strictly typed so a value cannot be two types at once as seen in JavaScript (and TypeScript).

---

<div class="post-metadata">

**Author:** ![ildefons](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ildefons/32/2638_2.png) [@ildefons](https://forum.dfinity.org/u/ildefons)\
**Post date:** [October 18, 2023, 10:30am UTC](https://forum.dfinity.org/t/how-to-call-and-declare-a-method-that-needs-to-receive-a-vector-of-number-and-text-values/23858/4 "2023-10-18T10:30:30Z")

</div>

How would look like the declaration of the method? Could you give me an example of how I would call the final method to send a vector like [1;“y”;3; “x”]?

---

<div class="post-metadata">

**Author:** ![sea-snake](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/sea-snake/32/7243_2.png) [@sea-snake](https://forum.dfinity.org/u/sea-snake)\
**Post date:** [October 18, 2023, 10:33am UTC](https://forum.dfinity.org/t/how-to-call-and-declare-a-method-that-needs-to-receive-a-vector-of-number-and-text-values/23858/5 "2023-10-18T10:33:56Z")

</div>

Both record and variant are the same in js, an object with key(s) and their values. Only difference is that a variant is a single key with a value and a record is all keys with their values.

For a variant vec with text and nat:

```auto
vec variant { a: text; b: nat }

```

JavaScript array:

```auto
[{a: 'hello''}, {b: 123}]

```

See the reference in the docs for all candid types and their types in different languages: [Candid reference | Internet Computer](https://internetcomputer.org/docs/current/references/candid-ref)

---

<div class="post-metadata">

**Author:** ![ildefons](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ildefons/32/2638_2.png) [@ildefons](https://forum.dfinity.org/u/ildefons)\
**Post date:** [October 18, 2023, 12:33pm UTC](https://forum.dfinity.org/t/how-to-call-and-declare-a-method-that-needs-to-receive-a-vector-of-number-and-text-values/23858/6 "2023-10-18T12:33:04Z")

</div>

Could you tell me what is my error declaring and using the variant below?

```auto
import Int "mo:base/Text";
import Nat "mo:base/Nat";

type dataMember = {
    #number : Nat; // variant 
    #symbol : Text; 
  };
var actor_data: [dataMember] = [1,"2",3];

```

---

<div class="post-metadata">

**Author:** ![sea-snake](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/sea-snake/32/7243_2.png) [@sea-snake](https://forum.dfinity.org/u/sea-snake)\
**Post date:** [October 18, 2023, 12:41pm UTC](https://forum.dfinity.org/t/how-to-call-and-declare-a-method-that-needs-to-receive-a-vector-of-number-and-text-values/23858/7 "2023-10-18T12:41:25Z")

</div>

Valid syntax should be:

```plaintext
import Int "mo:base/Text";
import Nat "mo:base/Nat";

type DataMember = {
    #number : Nat; // variant 
    #symbol : Text; 
  };
var actor_data: [DataMember] = [#number(1), #symbol("2"), #number(3)];

```

---

<div class="post-metadata">

**Author:** ![ildefons](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ildefons/32/2638_2.png) [@ildefons](https://forum.dfinity.org/u/ildefons)\
**Post date:** [October 18, 2023, 12:57pm UTC](https://forum.dfinity.org/t/how-to-call-and-declare-a-method-that-needs-to-receive-a-vector-of-number-and-text-values/23858/8 "2023-10-18T12:57:36Z")

</div>

How would it look a dfx call to a method accepting a vector of this variant?  
This one does not wok:  
`dfx canister call backend mymethod '(vec {1; "2"; 3})'`

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [October 18, 2023, 1:27pm UTC](https://forum.dfinity.org/t/how-to-call-and-declare-a-method-that-needs-to-receive-a-vector-of-number-and-text-values/23858/9 "2023-10-18T13:27:30Z")

</div>

Try `dfx canister call backend mymethod '(vec { variant { number = 1}; variant { symbol = "2" }; variant { number = 3}; )'`

---

<div class="post-metadata">

**Author:** ![ildefons](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ildefons/32/2638_2.png) [@ildefons](https://forum.dfinity.org/u/ildefons)\
**Post date:** [October 19, 2023, 10:25am UTC](https://forum.dfinity.org/t/how-to-call-and-declare-a-method-that-needs-to-receive-a-vector-of-number-and-text-values/23858/10 "2023-10-19T10:25:53Z")

</div>

I have complicated a bit the methjod declaration:  
`setData: (vec vec dataMember) -> (vec dataMember);`

so now I would like to accept data object like:

```auto
[[#number(1), #number(3), #symbol("2")],
 [#number(2), #number(2), #symbol("1")],
 [#number(3), #number(3), #symbol("2")],
 [#number(4), #number(2), #symbol("1")]];

```

I have tried the following dfx call:  
`dfx canister call motokolearn_backend setData '(vec { vec {variant {number=1}; variant {symbol="2"}; variant {number=3}}; vec {variant {number=2}; variant {symbol="2"}; variant {number=3}}; vec {variant {number=3}; variant {symbol="2"}; variant {number=3}}})'`

But I get an error:

```auto
Error: Failed update call.
Caused by: Failed update call.
  The replica returned a replica error: Replica Error: reject code DestinationInvalid, reject message Canister bkyz2-fmaaa-aaaaa-qaaaq-cai has no update method 'setTrainingData (vec { vec {variant {number=1}; variant {symbol="2"}; variant {number=3}}; vec {variant {number=2}; variant {symbol="2"}; variant {number=3}}; vec {variant {number=3}; variant {symbol="2"}; variant {number=3}}}) ', error code None

```

Could you point me out what am I doing wrong?

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [October 19, 2023, 11:05am UTC](https://forum.dfinity.org/t/how-to-call-and-declare-a-method-that-needs-to-receive-a-vector-of-number-and-text-values/23858/11 "2023-10-19T11:05:37Z")

</div>

Your dfx call and the error message don’t match up. Can you check the invocation again? It sounds like you accidentally had `'setTrainingData ` instead of `setData '` in your call

---

<div class="post-metadata">

**Author:** ![ildefons](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ildefons/32/2638_2.png) [@ildefons](https://forum.dfinity.org/u/ildefons)\
**Post date:** [October 19, 2023, 11:13am UTC](https://forum.dfinity.org/t/how-to-call-and-declare-a-method-that-needs-to-receive-a-vector-of-number-and-text-values/23858/12 "2023-10-19T11:13:26Z")

</div>

```auto
$ dfx canister call motokolearn_backend setTrainingData '(vec { vec {variant {number=1}; variant {symbol="2"}; variant {number=3};}; vec {variant {number=2}; variant {symbol="2"}; variant {number=3};}; vec {variant {number=3}; variant {symbol="2"}; variant {number=3};};})' 
Error: Failed update call.
Caused by: Failed update call.
  The replica returned a replica error: Replica Error: reject code DestinationInvalid, reject message Canister bkyz2-fmaaa-aaaaa-qaaaq-cai has no update method 'setTrainingData (vec { vec {variant {number=1}; variant {symbol="2"}; variant {number=3};}; vec {variant {number=2}; variant {symbol="2"}; variant {number=3};}; vec {variant {number=3}; variant {symbol="2"}; variant {number=3};};}) ', error code None

```

Actually, the right method name is “setTrainingData”. I think that it is something else… I think is related to the right way to nest vectors of variants but I cannot figure out the right way. Can you see any error in the parameter format?

---

<div class="post-metadata">

**Author:** ![ildefons](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ildefons/32/2638_2.png) [@ildefons](https://forum.dfinity.org/u/ildefons)\
**Post date:** [October 19, 2023, 11:36am UTC](https://forum.dfinity.org/t/how-to-call-and-declare-a-method-that-needs-to-receive-a-vector-of-number-and-text-values/23858/13 "2023-10-19T11:36:16Z")

</div>

the right way:

`dfx canister call motokolearn_backend setTrainingData '(vec { vec {variant {number=1}; variant {number=11}; variant {symbol="1"};}; vec {variant {number=2}; variant {number=21}; variant {symbol="2"};}; vec {variant {number=3}; variant {number=31}; variant {symbol="3"};};})'`
