Hello!
I want to call find()
on an Array that is stored as a value in a HashMap, but I get a type error.
Why can’t I call find?
[Error]
Stderr:
type error [M0072], field find does not exist in type [Token]
[Code]
import Array "mo:base/Array";
import Buffer "mo:base/Buffer";
import HashMap "mo:base/HashMap";
import Principal "mo:base/Principal";
import T "types"; // public type Token = Principal;
module {
public class Faucet() {
private let TOTAL_FAUCET_AMOUNT : Nat = 100_000;
private let FAUCET_AMOUNT : Nat = 1_000;
private var amount_counter : Nat = 0;
var faucet_book = HashMap.HashMap<Principal, [T.Token]>(
0,
Principal.equal,
Principal.hash,
);
public func checkDistribution(user : Principal, token : T.Token) : T.FaucetReceipt {
if (amount_counter >= TOTAL_FAUCET_AMOUNT) {
return (#Err(#InsufficientToken));
};
switch (faucet_book.get(user)) {
case null {
return #Ok(FAUCET_AMOUNT);
};
case (?tokens) {
switch (tokens.find(token)) { // Error
case null {
return #Ok(FAUCET_AMOUNT);
};
case (?token) return #Err(#AlreadyGiven);
};
};
};
};
public func addUser(user : Principal, token : T.Token) {
amount_counter += FAUCET_AMOUNT;
switch (faucet_book.get(user)) {
case null {
let new_data = Array.make<T.Token>(token);
faucet_book.put(user, new_data);
};
case (?tokens) {
let buff = Buffer.Buffer<T.Token>(2);
for (token in tokens.vals()) {
buff.add(token);
};
faucet_book.put(user, buff.toArray());
};
};
};
};
};
Thank you in advance for your response.