In my next.js app I am trying to upload image from form. But I keep getting an error:
[Error] Unhandled Promise Rejection: TypeError: Load failed
And the file doesn't upload to supabase.
I checked the image variable via console, it is a File object. Here is how I do it, I followed the tutorial in documentation:
import * as Form from "@radix-ui/react-form";
import { createClient } from "@supabase/supabase-js";
const supabase = createClient(
"url",
"api-key",
);
export default function Warehouses() {
async function uploadImage(e) {
const image = e.files[0];
const { data, error } = await supabase.storage
.from("images")
.upload("/", image);
if (data) {
console.log(data);
} else {
console.log(error);
}
}
const onSubmit = (event) => {
uploadImage(event.target.images);
};
return (
<div>
<Form.Root className="FormRoot" onSubmit={onSubmit}>
<Form.Field className="FormField" name="images">
<div
style={{
display: "flex",
alignItems: "baseline",
justifyContent: "space-between",
}}
>
<Form.Label className="FormLabel">
Upload Images
</Form.Label>
</div>
<Form.Control asChild>
<input
type="file"
id="images"
name="images"
multiple
/>
</Form.Control>
</Form.Field>
<Form.Submit asChild>
<Button>Create</Button>
</Form.Submit>
</Form.Root>
</div>
);
}
I will appreciate any help!
UPDATE
I get this error in Safari browser also:
[Error] Fetch API cannot load https://my-url/storage/v1/object/images/2 due to access control checks.
Here is what I get from console.log(image) inside uploadImage():
I also tried to check the connection to supabase with this code:
export async function getServerSideProps() {
const buckets = await supabase.storage.listBuckets();
console.log(buckets);
return {
props: {
bucketList: buckets,
},
};
}
But I get this in the console: ``` { data: [], error: null } ``
Below are the policies to my supabase bucket "images":
Here is the first (select) policy edit window:
Here is how the policies were added to my bucket: