How to turn a Blob into an array in Motoko?

Does anyone know why I’m getting

type error [M0072], field toArray does not exist in type Blob?

The docs say it does exist:

public shared (msg) func create_user() : async User {
    // Get caller principal
    let callerId = msg.caller;
    let principal_blob : Blob = callerId.toBlob();
    let principal_array : [Nat8] = principal_blob.toArray();

and why is it treating it as a field instead of a function?

Alternatively is there a better to get the principal’s array from inside an authenticated call to a function?

Not sure if my syntax ever worked or if I just made it up but it seems to be Blob.toArray(principal_blob) , etc.

As a quick PSA, both the Motoko VS Code extension and Motoko Playground provide value conversion code snippets such as blob-2-array, array-2-buffer, etc. It’s also easy to suggest or contribute a snippet if you think something is missing, since this is a crowdsourced feature. Cheers!

1 Like

That’s great. I have the extension but hadn’t noticed.

1 Like