How to handle "MongoServerError" which is causing server crash

60 views Asked by At

How to handle MongoServerError: you are over your space quota, using 514 MB of 512 MB
in nodejs server while uploading a file using multer-gridfs-storage library

Here is my code:

const storage = new GridFsStorage({
    url: process.env.MONGO_CONN_URL,
    file: (req, file) => {
      return new Promise( async (resolve, reject) => {

        const filename = file.originalname;
        const shortname = await hashName();

        console.log(req.body);

        const expTime = new Date(req.body.expiryTime);
        const noOfDown = parseInt(req.body.noOfDownload);
        const isPublic = req.body.isPublic==='true';
        const passwordValue = req.body.password;

        var uploadedBy = 'anonymous';
        if(req.cookies!==undefined && req.cookies.pass!==undefined){
          try{
            const token = req.cookies.pass;
            const payload=jwt.verify(token,process.env.JWT_PRIVATE_KEY);
            uploadedBy = payload.userId;
          }catch(err){
            console.log('catch block executed');
            
          }
        }

        const fileInfo = {
          filename: filename,
          metadata:{
            shortname: shortname,
            expiryTime: expTime,
            noOfDownload: noOfDown,
            isPublic: isPublic,
            password: passwordValue,
            uploadedBy: uploadedBy
          },
          bucketName: "newBucket"
        };
        resolve(fileInfo);
      });
    }
  });  

  const upload = multer({
    storage
  });

app.post('/upload',upload.single("fileUpload"),async (req,res)=>{

    const file = req.file;
    
    console.log("File has been uploaded. File   id: "+file.metadata.shortname);
    res.send(file.metadata.shortname)

});

Github link: https://github.com/Dy-123/File_Sharing/blob/master/backend/index.js

I tried storage.on streamError and dbError but it is not working

0

There are 0 answers