Hi,
have asked this question on Discord, Kyle redirected me to the IC-Avatar example , but can get my head around how to communicate with the asset canister which contains index.html and all website files.
I would like to get the content of an asset file into a variable in the backend canister.
I have implemented this simple code but await returns Misplaced await…
import Time “mo:base/Time”;
import T “mo:assets/Types”;
import AssetCanister “canister:app_frontend”;
The problem here is that you can’t await from the actor initialization code, only from its methods. This is actually a restriction of the internet computer, enforced at compile time in Motoko.
Assuming the code is otherwise correct, putting it in a shared function body should help.
Hi! Thank you again for your response.
I have tried many things this week-end, without success. I have a hard time grasping the async/await concept
Is init() a system function that is call when creating a class ?
Any exemple exist of a complet asset canister with some docs and working code ?
Thank you
I think your code is reasonably close to working, but you can only await in certain contexts, and the main sequence of declaration in an actor isn’t one of them.
Some untested editing:
import AssetCanister "canister:app_frontend"; // was this missing?
actor {
Debug.print("0-Main execute");
type StructCanisterContent = {
content_type : Text;
encodings : [{
content_encoding : Text;
length : Nat;
modified : Time.Time;
sha256 : ?Blob;
}];
key : T.Key;
};
var fileList : [StructCanisterContent] = [];
public func show() : async Text {
// initialize fileList if empty;
if (fileList == []) {
fileList := await AssetCanister.list({}); // pass an empty record.
};
debug_show fileList // convert fileList to a string for display
};
}
Then call show using dfx or the playground (if deploying to that).
Hi!
Work great from dfx, but I need to call show() from the actor, something like :
var fileList : [StructCanisterContent] = ;
public func show() : async Text {
// initialize fileList if empty;
if (fileList == []) {
fileList := await AssetCanister.list({}); // pass an empty record.
};
debug_show fileList // convert fileList to a string for display
};
let index = show(); // Aint working, if I add await, still dont get it.
}
I have tried to include the code in a module without success.
Dont know if it make sens, but I’m currently using HTMX with Motoko like an MVC, so I don’t need to call the candid files like in Javascript, the front end is HTML and the backend only Motoko.
Hi! Does the init() func supposed to be fired on instantiation of the class or when the actor is called or run like in the case of the main.mo when the actor is being executed ? Searched the documentation for Constructor or init() without success.