Problem in sending arguments to the function via command line and js backend

I have copied the phonebook code from the motoko playground. The code is like,

import Map "mo:base/HashMap";
import Text "mo:base/Text";

actor {

  type Name = Text;
  type Phone = Text;

  type Entry = {
    desc: Text;
    phone: Text;
  };

  let phonebook = Map.HashMap<Name, Entry>(0, Text.equal, Text.hash);

  public func insert(name : Name, entry : Entry): async Text {
    phonebook.put(name, entry);
    return "Contact added successfull";
  };

  public query func lookup(name : Name) : async ?Entry {
    phonebook.get(name)
  };
};

How can I call the function insert, which have the arguments Name and Entry, where entry is defined as

type Entry = {
    desc: Text;
    phone: Text;
  };

I recommend you look at the Candid reference and maybe the Candid intro.

In your case, the function call would look like this:

❯ dfx canister call hello_backend insert '("name text", record {desc = "desc"; phone = "phone"})'
("Contact added successfull")
1 Like

Thanks for the solution Severin.
Can you also show me how can I call that function using js? (from my backend_js file, index.js)

const name = document.getElementById("name").value.toString();

  const desc = document.getElementById("desc").value.toString();

  const phone_number = document.getElementById("phone_number").value.toString();


  // Interact with actor, calling the insert method
  const result = await proj3_backend.insert(name, desc, phone_number);

I have the called function like the above, but the interface got stuck whenever I call the function from the frontend, with no activity in Network part…

I’m no good at js, so don’t take my word as gospel.

await proj3_backend.insert(name, desc, phone_number);

does not match the signature

public func insert(name : Name, entry : Entry)

The signature expects two arguments, one of which is an object with two elements, your function call tries to call a function with three arguments.

Using this example, I would guess that something like this should work:

let my_entry = {
    desc: "my text",
    phone: "my phone"
};
let result = await proj3_backend.insert("my_name", my_entry);
1 Like

Thank you Severin. That totally works for me. Thanks a lot.