devconcept / multer-gridfs-storage

🍃 GridFS storage engine for Multer to store uploaded files directly to MongoDb
MIT License
238 stars 69 forks source link

uncaughtException: Cannot read properties of undefin ed (reading '_id') #560

Open sanjarbek-ismatov opened 1 year ago

sanjarbek-ismatov commented 1 year ago

image_1 image_2

cleaverm commented 1 year ago

I started getting this error last week after updating a couple of MongoDB-related dependencies in a project. mongodb is at 4.17.1 in the project but Yarn was installing some 6.x.x versions for other dependencies.

Putting this in package.json has resolved it for me:

  "resolutions": {
    "mongodb": "4.17.1"
  },
theBGuy commented 1 year ago

Also getting this error, changing the mongodb version didn't fix it for me. I have tracked down the error (at least mine) to fromMulterStream method. The value passed to emitFile when the finish event is triggered is undefined.

sanjarbek-ismatov commented 1 year ago

Also getting this error, changing the mongodb version didn't fix it for me. I have tracked down the error (at least mine) to fromMulterStream method. The value passed to emitFile when the finish event is triggered is undefined.

It helped me, it's working correctly

ghost commented 1 year ago

Hi, this error still is problem for someone. May you help me ?

[1] TypeError: Cannot read properties of undefined (reading '_id') [1] at GridFSBucketWriteStream.emitFile (/home/cppdozer/Projects/encore/server/node_modules/multer-gridfs-storage/src/gridfs.ts:353:12) [1] at GridFSBucketWriteStream.emit (node:events:529:35) [1] at GridFSBucketWriteStream.emit (node:domain:489:12) [1] at finish (node:internal/streams/writable:748:10) [1] at processTicksAndRejections (node:internal/process/task_queues:82:21) [1] [nodemon] app crashed - waiting for file changes before starting...

ghost commented 1 year ago

Hi, this error still is problem for someone. May you help me ?

[1] TypeError: Cannot read properties of undefined (reading '_id') [1] at GridFSBucketWriteStream.emitFile (/home/cppdozer/Projects/encore/server/node_modules/multer-gridfs-storage/src/gridfs.ts:353:12) [1] at GridFSBucketWriteStream.emit (node:events:529:35) [1] at GridFSBucketWriteStream.emit (node:domain:489:12) [1] at finish (node:internal/streams/writable:748:10) [1] at processTicksAndRejections (node:internal/process/task_queues:82:21) [1] [nodemon] app crashed - waiting for file changes before starting...

I just installed mongodb module in my project and problem solved immediately.

jasonhis commented 1 year ago

If you are using yarn, adding this to the package.json will fix it.

  "resolutions": {
    "multer-gridfs-storage/mongodb": "^5.9.1"
  },
anshvert commented 1 year ago

If you are using yarn, adding this to the package.json will fix it.

  "resolutions": {
    "multer-gridfs-storage/mongodb": "^5.9.1"
  },

This Worked for me. TY

ARedmanDodd commented 1 year ago

I just had this same issue, however Im not using Yarn. To resolve I needed to install mongodb v5.9.1

npm install mongodb@5.9.1

This instantly fixed the issue

AmanSingh44 commented 1 year ago

I just had this same issue, however Im not using Yarn. To resolve I needed to install mongodb v5.9.1

npm install mongodb@5.9.1

This instantly fixed the issue

This worked for me, before trying this i didn't had the mongodb module in my project, latest version also works. Thank you, can finally upload images

meme8383 commented 1 year ago

Resolved by #561

meme8383 commented 1 year ago

I made a fork if you would like to use the latest versions of mongoose/mongodb, add "multer-gridfs-storage": "meme8383/multer-gridfs-storage#f82252c" to your package.json (add commit or maintain your own fork for security). Tested on mongodb 6.3.0 and mongoose 8.0.1.

meme8383 commented 1 year ago

Fixed another issue relating to file count limits and mongodb update in commit 32d2198. Feel free to use my fork or make your own as it appears this package is no longer maintained.

Arko0809 commented 11 months ago

If you dont have mongodb installed in your project and multer and multer-gridfs-storage installed; you will get error while installing mongodb by multer and multer-gridfs-storage. Step 1 : npm uninstall multer multer-gridfs-storage

Step 2 : npm i mongodb@5.9.1 (In my case only this version worked. Check with the latest version first otherwise this is your only option)

Step 3: npm i multer-gridfs-storage

Step 4: npm i multer

Aakash-bhandari commented 10 months ago

I just had this same issue, however Im not using Yarn. To resolve I needed to install mongodb v5.9.1 npm install mongodb@5.9.1 This instantly fixed the issue

This worked for me, before trying this i didn't had the mongodb module in my project, latest version also works. Thank you, can finally upload images

Can you share you Package.json file

amjad-awan commented 10 months ago

i have this same error, my dependencies are "mongoose": "^8.0.4", "multer": "^1.4.4", "multer-gridfs-storage": "^5.0.2", D:\All\MearStackProject\music-app\backend\node_modules\multer-gridfs-storage\lib\gridfs.js:306 id: f._id, ^

TypeError: Cannot read properties of undefined (reading '_id') at GridFSBucketWriteStream.emitFile (D:\All\MearStackProject\music-app\backend\node_modules\multer-gridfs-storage\lib\gridfs.js:306:31)

arunwad commented 10 months ago

I have the same issue and want to try the fix in meme8383's fork. I am a newbie and am reading up on how to do so. How does a bug fix get incorporated in the latest version that is downloadable using npm?

meme8383 commented 10 months ago

I have the same issue and want to try the fix in meme8383's fork. I am a newbie and am reading up on how to do so. How does a bug fix get incorporated in the latest version that is downloadable using npm?

The author has to merge a fix and publish it. I was going to rewrite this library, but I switched to using an actual cloud provider for cloud storage instead. You can use my fork by setting the version to meme8383/multer-gridfs-storage#32d2198 in your package.json.

arunwad commented 10 months ago

Thanks Meme8383, I cloned your fork and used it in my app and it works, .... problem solved.

ahsanch445 commented 10 months ago

I have the same issue and want to try the fix in meme8383's fork. I am a newbie and am reading up on how to do so. How does a bug fix get incorporated in the latest version that is downloadable using npm?

The author has to merge a fix and publish it. I was going to rewrite this library, but I switched to using an actual cloud provider for cloud storage instead. You can use my fork by setting the version to meme8383/multer-gridfs-storage#32d2198 in your package.json.

can it work on production level?

meme8383 commented 10 months ago

I have the same issue and want to try the fix in meme8383's fork. I am a newbie and am reading up on how to do so. How does a bug fix get incorporated in the latest version that is downloadable using npm?

The author has to merge a fix and publish it. I was going to rewrite this library, but I switched to using an actual cloud provider for cloud storage instead. You can use my fork by setting the version to meme8383/multer-gridfs-storage#32d2198 in your package.json.

can it work on production level?

That is for you to decide. This library has not been tested thoroughly with the most recent versions of these libraries. If it is necessary, I would recommend maintaining your own fork in case other issues need fixing. Using my fork with the commit reference should work fine as long as you thoroughly test your application.

However, I don't think you should be using MongoDB for file storage in production anyways.

alanpurple commented 8 months ago

is this repository dead? why no update over 3 years?

kumar11473 commented 8 months ago

is this repository dead? why no update over 3 years?

Can't say yes or no but this issue isn't resolved yet. I'm still getting this error with latest version of Mongodb. Mongodb@5.9.1 works fine .

Screenshot 2024-03-27 212400

meme8383 commented 8 months ago

is this repository dead? why no update over 3 years?

Can't say yes or no but this issue isn't resolved yet. I'm still getting this error with latest version of Mongodb. Mongodb@5.9.1 works fine .

Screenshot 2024-03-27 212400

You can use my branch by adding meme8383/multer-gridfs-storage to your package.json, I fixed that and another issue. However, not a great fix, since optimally this should be rewritten and thoroughly tested for the newest versions.

alanpurple commented 8 months ago

I admire @meme8383 's output, but this, https://github.com/lykmapipo/mongoose-gridfs, and https://github.com/aheckmann/gridfs-stream all should be properly updated to the latest mongodb driver for proper long-term projects

kumar11473 commented 8 months ago

is this repository dead? why no update over 3 years?

Can't say yes or no but this issue isn't resolved yet. I'm still getting this error with latest version of Mongodb. Mongodb@5.9.1 works fine . Screenshot 2024-03-27 212400

You can use my branch by adding meme8383/multer-gridfs-storage to your package.json, I fixed that and another issue. However, not a great fix, since optimally this should be rewritten and thoroughly tested for the newest versions.

Its working. Thanks @meme8383. Can you please brief how you fixed it and came to know what function or file need to be fixed?

meme8383 commented 8 months ago

is this repository dead? why no update over 3 years?

Can't say yes or no but this issue isn't resolved yet. I'm still getting this error with latest version of Mongodb. Mongodb@5.9.1 works fine . Screenshot 2024-03-27 212400

You can use my branch by adding meme8383/multer-gridfs-storage to your package.json, I fixed that and another issue. However, not a great fix, since optimally this should be rewritten and thoroughly tested for the newest versions.

Its working. Thanks @meme8383. Can you please brief how you fixed it and came to know what function or file need to be fixed?

https://github.com/devconcept/multer-gridfs-storage/compare/master...meme8383:multer-gridfs-storage:master Pretty simple fixes, but there are bound to be more issues. I was going to rewrite the library for the updated packages but we decided to switch away from using Mongo for file storage. Test your project thoroughly if you use this.

vanitharc12 commented 8 months ago

Hi , Its throwing the above error in latest version or downgraded versions as well ."mongoose": "^8.2.4", "multer": "^1.4.5-lts.1", "multer-gridfs-storage": "^5.0.2", Does anyone resolved the issue with compatible versions? Can you tell me the versions used ?

KoushikBaagh commented 7 months ago

I just had this same issue, however Im not using Yarn. To resolve I needed to install mongodb v5.9.1

npm install mongodb@5.9.1

This instantly fixed the issue

Working on date: 02/05/2024. This issue still persists. only way is to downgrade to mongodb, 5.9.1

krishna102001 commented 5 months ago

By downgrading the mongoDB version issue is fixed

npm i mongodb@5.9.1

This worked for me also

krishna102001 commented 5 months ago

Hi , Its throwing the above error in latest version or downgraded versions as well ."mongoose": "^8.2.4", "multer": "^1.4.5-lts.1", "multer-gridfs-storage": "^5.0.2", Does anyone resolved the issue with compatible versions? Can you tell me the versions used ?

just downgrade the multer version

npm install multer@1.4.2 --save

because there is dependency conflict between multer and multer-gridfs-storage. Specifically, multer-gridfs-storage@5.0.2 requires multer@^1.4.2.

prince-sunsara commented 5 months ago

Thank you so much, it's working

Lilianada commented 4 months ago

I use yarn I downgraded the version of mongodb and added resolutions but none of it works "mongodb": "5.9.1", "mongoose": "^5.13.14", "resolutions": { "multer-gridfs-storage/mongodb": "^5.9.1" }

Uncaught Exception: Cannot read properties of undefined (reading '_id') TypeError: Cannot read properties of undefined (reading '_id') at GridFSBucketWriteStream.emitFile

prince-sunsara commented 4 months ago

downgrade multer version

npm install multer@1.4.2 --save

cpalmer-ios commented 2 months ago

I found the error was occurring because the _id was coming back as undefined. I made a PR to use the id coming back from Mongo DB in the fromMulterStream function: https://github.com/devconcept/multer-gridfs-storage/pull/586

Had some trouble compiling the code (ts errors) so edited the system generated file directly so you'd see what I mean

the ts errors were coming from the following modules and file:

Found 7 errors in 5 files.

Errors  Files
     1  node_modules/@types/eslint/helpers.d.ts:1
     1  node_modules/@types/markdown-it/lib/index.d.ts:151
     1  node_modules/@types/mongodb/index.d.ts:58
     3  src/gridfs.ts:216
     1  ../../../../node_modules/@types/eslint/helpers.d.ts:1

Thanks, hope it helps !