nikhildhokaN
Convex Community14mo ago
11 replies
nikhildhoka

How to retrieve files from the Storage in Convex

code:

export const getPackageById = query({
  args: { packageId: v.id("packageTable") }, // Validate that packageId is an ID from "packageTable"
  handler: async (ctx: any, args: any) => {
    const identity = await ctx.auth.getUserIdentity();
    if (!identity) {
        throw new Error("Unauthorized");
    }
    const pkg = await ctx.db.get(args.packageId); // Fetch the package by ID
    pkg.metadata.ID = pkg._id;
    if (!pkg) {
      throw new Error(`Package with ID ${args.packageId} not found.`);
    }
    const fileStorageId = (pkg.data.Content) ! as Id<"_storage">;
    console.log('File Storage ID:', fileStorageId);
    const blob = await ctx.storage.get(fileStorageId);
    console.log("blob:  ", blob);
    if (!blob) {
      return new Response("Blob not found", { status: 404 });
    }
    pkg.data.Content = await blobToBase64(blob);
    return pkg;
  },
});


error:
Dec 07, 19:53:04
Q
queries/packageTable:getPackageById
failure
33ms
Uncaught TypeError: ctx.storage.get is not a function
at handler (../../convex/queries/packageTable.ts:37:31)
Was this page helpful?