1

I want to query the task from storage. Here is the Task struct and StorageMap

#[derive(Clone, Encode, Decode, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen, Default)]
    #[scale_info(skip_type_params(T))]
    pub struct Task<T:Config>{
        pub id: [u8; 16],
        pub state: u8,
        pub owner: T::AccountId,
        pub description: BoundedVec<u8, ConstU32<100>>,
        // pub deadline
    }

#[pallet::storage]
    #[pallet::getter(fn gettasks)]
    pub(super) type Tasks<T: Config> = StorageMap<_, Twox64Concat, [u8; 16], Task<T>>;

when creating task, I enter "Hi" for the field description. But when query by taskId, It shows

{"id":"0x....","state":1,"owner":"5Grw...","description":"0x6869"}

How to make the description showing in raw text?

1 Answer 1

0

Substrate runtimes use u8 data for strings, whereas Rust normally uses String.

You can convert it back from raw like this:

// Some description queried from storage.
let description: [u8; 16] = [0x68, 0x69, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];

match String::from_utf8(description.to_vec()) {
    Ok(s) => println!("{}", s),
    Err(e) => println!("Error: {}", e),
}
// prints 'hi'

Note that this will not work within the runtime code, since it cannot use strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.