1

I am following online tutorial on youtube which is about building social network using react as front end and firebase functions as back end and I keep getting this error using the uploadImage route and I have no idea where the problem is, if someone could explain why I am having this error.

exports.uploadImage=(req,res)=>{
    const BusBoy=require('busboy')
    const path=require('path')
    const os=require('os')
    const fs =require('fs')

    let imageFileName;
    let imageToBeUploaded={}

    const busboy = new BusBoy({ headers: req.headers })

    busboy.on('file',(fieldname, file, filename, encoding, mimetype)=>{
        // image.png
        const imageExtension = filename.split('.')[filename.split('.').length - 1]
         imageFileName = `${Math.round(Math.random()*100000000000)}.${imageExtension}`;
        const filepath = path.join(os.tmpdir(),imageFileName);
        imageToBeUploaded={filepath, mimetype};
        file.pipe(fs.createWriteStream(filepath))
    });
    busboy.on('finish',()=>{
        admin
        .storage()
        .bucket()
        .upload(imageToBeUploaded.filepath,{
            resumable:false,
            metadata:{
                metadata:{
                    contentType: imageToBeUploaded.mimetype
                }
            }
        })
        .then(()=>{
            const imageUrl = `https://firebasestorage.googleapis.com/v0/b/${config.storageBucket}/o/${imageFileName}?alt=media`
            return db.doc(`/users/${req.user.handle}`).update({imageUrl})
        })
        .then(()=>{
            return res.json({message:'Image uploaded Successfully'})
        })
        .catch(err=>{
            console.error(err);
            return res.status(500).json({error:err.code})
        })
    })
    busboy.end(req.rawBody)
}
pzaenger
  • 11,381
  • 3
  • 45
  • 46
Mostafa Sobh
  • 29
  • 1
  • 5
  • 3
    The error means that some text that is supposed to be JSON is not JSON. If this is the code that generates the output that is not JSON then capture it somehow (in the browser, maybe) and put it into the question. The code itself does not help much. – axiac Jun 09 '20 at 16:29

0 Answers0