Type error, option pattern cannot consume expected type async<$List> (?Company)

Hi everyone!

I have 2 files one is database.mo and other is main.mo. I tried to follow linkedup logic.

database.mo:

public func listCompanies(userid: Principal): async ?Company {
    if(isStudent(userid)){
        func isEq(company: Company): Bool {
            company.owner == userid;
        };

        switch (Array.find<Company>(personalList, isEq)) {
            case(null) {
                 return null;
            };
            case(?exists) {
                return ?{
                    id = exists.id;
                    owner = exists.owner;
                    companyID = exists.companyID;
                };
            };
        };
    } else {return null;}
};

main.mo:

public shared(msg) func List(): async ?Company{
directory.listCompanies(msg.caller);
};

I tried to do something like this in main.mo file because I had some errors:

public shared(msg) func List(): async ?Company{
   switch (directory.listCompanies(msg.caller))
    {
        case(?e){
            return ?{
            
            };
        };
    };
}; 

The error appears with the second try that I wrote above, and is this:

type error, option pattern cannot consume expected type
  async<$List> (?Company)

What I have to do to solve this error? where is my mistake?

Thank you!

You probably don’t want listCompanies() to have an async return in database.mo, so the first line there should be:

public func listCompanies(userid: Principal): ?Company {

1 Like

You use async methods in your main.mo actor because that’s your canister’s interface on the IC and it needs to be called asynchronously, but your database.mo is probably a module within your canister? In which case it doesn’t need this.

If listCompanies were async, you would need to await the return value in the switch statement:

switch (await directory.listCompanies(msg.caller))

3 Likes

Thank you for your answer and for your willing to help! :smiley: These answers helped me a lot!

2 Likes