I have a NEAR smart contract that keeps a HashMap of Veggie records. My most basic accessor method get_veggie(vid), which looks up and returns one Veggie record, passes unit tests but fails in the deployed contract. It panics with 'veggie does not exist' when I send one of the keys returned by another accessor method, get_veggie_keys().
// this method returns an array of u64 keys:
pub fn get_veggie_keys(&self) -> Vec<TokenId> {
self.veggies.keys().cloned().collect()
}
// but this method panics when I give it one of those keys:
pub fn get_veggie(&self, vid: TokenId) -> Veggie {
let veggie = match self.veggies.get(&vid) {
Some(c) => {
c
},
None => {
env::panic(b"Veggie does not exist.")
}
};
veggie.clone()
}
I see this behavior when I call these methods from the NEAR CLI:
% near call --accountId $ACCOUNTID $CONTRACT_NAME get_veggie_keys
Scheduling a call: dev-1602786511653-5521463.get_veggie_keys()
Transaction Id FdWjevTsMD73eFPno41THrvrChfB9HDoLAozuiXsBwru
To see the transaction in the transaction explorer, please open this url in your browser
https://explorer.testnet.near.org/transactions/FdWjevTsMD73eFPno41THrvrChfB9HDoLAozuiXsBwru
[ 3469591985938534000, [length]: 1 ]
%
% near call --accountId $ACCOUNTID $CONTRACT_NAME get_veggie '{"vid":3469591985938534000}'
Scheduling a call: dev-1602786511653-5521463.get_veggie({"vid":3469591985938534000})
Receipt: 68ahRQyNN7tzAQMbguCEy83ofL6S5mv3iLVmmN2NH8gh
Failure [dev-1602786511653-5521463]: Error: Smart contract panicked: Veggie does not exist.
An error occured [...]
How is this behavior different in the contract than in unit tests? Am I calling the method wrong? Do I not understand HashMaps? Thanks for any advice. Maybe I'm making a Rust noob error, but I'm deeply puzzled here ...