Desperate for help with Trpc resolver.
How can i get resolve({ default: base64Data }); to only trigger once trpc has returned success or failure.
class MyUploadAdapter {
private loader: any;
private mutation: any;
constructor(loader, mutation) {
this.loader = loader;
this.mutation = mutation;
}
private async _uploadFile(file: File) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = async () => {
const base64Data = reader.result?.toString().split(',')[1] || '';
// mutate will automatically handle success and failure.
try {
await this.mutation.mutate({
base64Data,
});
resolve({ default: base64Data });
// Since mutate will handle success and failure,
// you don't need to manually check for errors or success here.
// Instead, just resolve with the base64Data,
// and CKEditor will handle the result.
} catch (error) {
// The error is caught here but it has already been handled by the onError handler.
reject(error);
}
};
reader.readAsDataURL(file);
});
}
}
10 Replies
i think youre looking for
mutation.mutateAsync()
thanks man i'll try that i actually messed up my conditional logic. I managed to get it working using state and then finally overriding the else to use the the resolver aswell. Gonna need to go back and try sort it.
Very ugly implementation
I’m about to go to bed but I’m a little unclear what you’re trying to do here
basically im sending off a base64 url to a trpc endpoint. getting image optimizedm uploaded to supabase then returning a image url.
But this particular code is for ckeditor seems to be the way they have gone about it
I’m on my phone and can’t open the text file
No worries tom if you have a chance later have a squizz. Think I got it now, need a cleaner version eventually but will figure it out.
Thanks
If you just want something to run once the mutation is done why don’t you use the onSuccess or the onSettled functions? (They go in at the object that is the second parameter of useMutation)
Thanks, had a bit of a brainfart but got it in the end.
We have all been there fore sure