Amiya Behera
Amiya Behera

Reputation: 2270

How to get a struct from view! call in near_sdk_sim?

I have a following method:

pub fn get_product(&self, product_id: U128) -> Product {
        let product_option = self.product_map.get(&product_id);
        match product_option {
            Some(product) => product,
            None => {
                panic!("No products for the id");
            }
        }
    }

Product struct:

#[derive(Default, BorshDeserialize, BorshSerialize)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct Product {
    pub user_id: u128,
    pub product_details_hash: String, 
}

When I call in near_sdk_sim I get error

let product:Product = view!(contract.get_product(1.into())).unwrap_json();

Error is:

called `Result::unwrap()` on an `Err` value: Error("EOF while parsing a value", line: 1, column: 0)

How can I get the struct from the view! call?

Upvotes: 1

Views: 31

Answers (1)

sirwillem
sirwillem

Reputation: 722

For future reference questions of this nature are better suited as an issuenear-sdk-rs .

My guess for this issue is that the call paniced and thus there was no value to deserialize. Try checking if it is okay.

let res = view!(contract.get_product(1.into()));
assert!(res.is_ok());

Upvotes: 1

Related Questions