Ana
Ana

Reputation: 31

uploading a file on supabase: error RequestInit: duplex option is required when sending a body

I'm trying upload a file on supabase with node.js and I get this error: RequestInit: duplex option is required when sending a body. I don't konw what it means. What am I doing wrong in my code?

router.post("/", async (req, res) => {
    const datosForm = req.body;
 
    const supabase = createClient(
        process.env.SUPABASE_URL,
        process.env.SUPABASE_KEY
    );

    const form = formidable({
        multiples: false,
        //uploadDir: "public/images",
        keepExtensions: true,
    })
    //await form.parse(req)
    form.parse(req, async(err, fields, files) => {
        if (err) {
            console.log("error: " + err);
        } else {
            console.log("files: ", files.foto.newFilename);
            let imagePath;
            try {
                const mascotaDB = new Mascota(fields);
                if(files.foto.originalFilename != ""){
                    const ext = path.extname(files.foto.filepath);
                    const newFileName = "image_"+Date.now()+ext;
                    console.log("newFileName: ", newFileName);
                    try{
                        const{data, error}=await supabase.storage.from("articulos").upload(
                            newFileName,    
                            fs.createReadStream(files.foto.filepath),   
                            {
                                cacheControl: '3600',
                                upsert: false,     
                                contentType: files.foto.type,  
                            }
                        );                    
                        imagePath = newFileName;
                        console.log("path: ", imagePath);
                        console.log("mascota: ", imagePath);
                        mascotaDB.path=imagePath;
                        
                        await mascotaDB.save()
        
                        res.redirect("/articulos") 
                    }catch(err){
                        console.log("error: "+err);
                    }
                    
                }
               
            
            } catch (error) {
                console.log(error)
            }
        }
        
    })

    
});

I'm not get the image I'm trying to upload in the bucket I created in supabase

Upvotes: 3

Views: 7028

Answers (2)

Jul Pod
Jul Pod

Reputation: 396

i made it work with converting the read file to a base64 string and then use the decode method to generate an array buffer (how to upload a base64 string is also covered in the official documentation of supabase: https://supabase.com/docs/reference/javascript/storage-from-upload?example=upload-file-using-arraybuffer-from-base64-file-data)

import { decode } from 'base64-arraybuffer';

const fileData = fs.readFileSync(zipFilePath);
const buffedInput = fileData.toString("base64");

// upload zip file to supabase
const res = await supabase.storage
              .from(bucketName)
              .upload(uploadPath, decode(buffedInput), { contentType: 'application/zip' }); 
    
console.log(res);

Upvotes: 0

Matteo Lancia
Matteo Lancia

Reputation: 51

The specific error refers to this

In the request, init argument requires property "duplex": "half"

Probably you have a version of NodeJS that is causing this, as seen here: https://github.com/nodejs/node/issues/46221

Upvotes: 4

Related Questions