我正在尝试更新向量,这是我的代码。
use near_sdk::collections::Map;
use near_sdk::collections::Vector;
#[near_bindgen]
#[derive(Default, BorshDeserialize, BorshSerialize)]
pub struct ProfileDetails {
profile_tags: Map<String, ProductList>,
}
#[near_bindgen]
#[derive(Default, BorshDeserialize, BorshSerialize, Debug)]
pub struct Product {
product_name: String,
product_details: String,
}
#[near_bindgen]
#[derive(Default, BorshDeserialize, BorshSerialize)]
pub struct ProductList {
products: Vector<Product>,
}
#[near_bindgen]
impl ProfileDetails {
pub fn set_profile(&mut self, product_name: String, product_details: String) {
let account_id = String::from("amiyatulu.test");
println!("{}", product_name);
let p = Product {
product_name,
product_details,
};
let id = account_id.clone().into_bytes();
let mut id_products = ProductList {
products: Vector::new(id),
};
id_products.products.push(&p);
self.profile_tags.insert(&account_id, &id_products);
}
pub fn push_product_to_profile(&mut self, product_name: String, product_details: String) {
let account_id = String::from("amiyatulu.test");
let p = Product {
product_name,
product_details,
};
let my_products_option = self.profile_tags.get(&account_id);
match my_products_option {
Some(mut my_products) => {
my_products.products.push(&p); //It doesn't update the state
self.profile_tags.insert(&account_id, &my_products);
println!("Hello myproducts push");
}
None => println!("Can't get the profile tag"),
}
}
问题是这个语句不会更新区块链的状态。
my_products.products.push(&p); //It doesn't update the state
因此,我在此语句中再次插入了向量。
self.profile_tags.insert(&account_id, &my_products);
这是正确的方法吗?会不会导致 ProductList 的积向量重复存储?如何获取向量状态并更新它?
完整代码在这里