{ const cursor = bucket.find({}); " /> { const cursor = bucket.find({}); " /> { const cursor = bucket.find({}); "/>

Why my server is crashing on frequent request for bucket.delete(files[i]._id); in mogodb-gridfs

39 Views Asked by At

For the below code when i make too frequent get request my app crashes :

app.get("/publicFiles", async (req,res) => {

  const cursor = bucket.find({});
  const files = await cursor.toArray();

  var publicFiles=[];
  for(var i=0;i<files.length;++i){
    console.log(files[i]);
    if(Date.parse(files[i].uploadDate)<=Date.now()){
      await bucket.delete(files[i]._id);
    }else if(files[i].metadata.isPublic){
      delete(files[i]._id);
      publicFiles.push(files[i]);
    }
  } 
  console.log("Number of public file send is: "+publicFiles.length);
  res.status(200).send(publicFiles);

})

The console.log of the above code is

File has been uploaded. File id: gk
{
  _id: new ObjectId("64b306fe442f05e21f459f00"),
  length: 25426160,
  chunkSize: 261120,
  uploadDate: 2023-07-15T20:52:22.440Z,
  filename: 'python-3.11.4-amd64.exe',
  contentType: 'application/x-msdownload',
  metadata: {
    shortname: 'bk',
    expiryTime: 2023-07-25T20:52:00.000Z,
    noOfDownload: 4,
    isPublic: true
  }
}
{
  _id: new ObjectId("64b30760442f05e21f459f64"),
  length: 25426160,
  chunkSize: 261120,
  uploadDate: 2023-07-15T20:53:59.385Z,
  filename: 'python-3.11.4-amd64.exe',
  contentType: 'application/x-msdownload',
  metadata: {
    shortname: 'ck',
    expiryTime: 2023-07-25T20:52:00.000Z,
    noOfDownload: 4,
    isPublic: true
  }
}
{
  _id: new ObjectId("64b30767442f05e21f459fc8"),
  length: 25426160,
  chunkSize: 261120,
  uploadDate: 2023-07-15T20:54:07.442Z,
  filename: 'python-3.11.4-amd64.exe',
  contentType: 'application/x-msdownload',
  metadata: {
    shortname: 'dk',
    expiryTime: 2023-07-25T20:52:00.000Z,
    noOfDownload: 4,
    isPublic: true
  }
}
{
  _id: new ObjectId("64b3076f442f05e21f45a02c"),
  length: 25426160,
  chunkSize: 261120,
  uploadDate: 2023-07-15T20:54:15.151Z,
  filename: 'python-3.11.4-amd64.exe',
  contentType: 'application/x-msdownload',
  metadata: {
    shortname: 'ek',
    expiryTime: 2023-07-25T20:52:00.000Z,
    noOfDownload: 4,
    isPublic: true
  }
}
{
  _id: new ObjectId("64b3076f442f05e21f45a02c"),
  length: 25426160,
  chunkSize: 261120,
  uploadDate: 2023-07-15T20:54:15.151Z,
  filename: 'python-3.11.4-amd64.exe',
  contentType: 'application/x-msdownload',
  metadata: {
    shortname: 'ek',
    expiryTime: 2023-07-25T20:52:00.000Z,
    noOfDownload: 4,
    isPublic: true
  }
}
C:\Users\d123\Desktop\fileSharing\backend\node_modules\mongoose\node_modules\mongodb\lib\gridfs\index.js:70
            throw new error_1.MongoRuntimeError(`File not found for id ${id}`);
                  ^

MongoRuntimeError: File not found for id 64b3076f442f05e21f45a02c
    at GridFSBucket.delete (C:\Users\d123\Desktop\fileSharing\backend\node_modules\mongoose\node_modules\mongodb\lib\gridfs\index.js:70:19)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async C:\Users\d123\Desktop\fileSharing\backend\index.js:185:7 {
  [Symbol(errorLabels)]: Set(0) {}
}

Why 64b3076f442f05e21f45a02c is being repeated.Does one post request will be executed once previous post get executed or post requests will start asynchronous if so then how to handle this case.

0

There are 0 best solutions below