javascript - multer-gridfs error " TypeError: Cannot read properties of undefined (reading '_id') &

I'm working on creating a WhatsApp clone and implementing a file sharing feature. Everything was g

I'm working on creating a WhatsApp clone and implementing a file sharing feature. Everything was going smoothly until I encountered this error that's causing my backend to crash. The strange thing is, I can see the file in MongoDB, but the error persists. I'm hoping someone can help me troubleshoot this issue.

Here's a summary of what's happening:

I have a file sharing feature in my WhatsApp clone app. When a user tries to share a file, it's saved to MongoDB. I can confirm that the file exists in the MongoDB database. However, the backend crashes with an error that I can't seem to resolve. I'm not sure where to begin troubleshooting this issue. Can anyone provide insights on what might be causing this problem? Is there a mon mistake I might be making when dealing with file sharing in a MongoDB database?

i cant post all my code here my question will be too long [here is my github link][1]

api.js

export const uploadFile = async (data) => {
    try {
        return await axios.post(`${url}/file/upload`, data);
    } catch (error) {
        console.log('Error while calling uploadfile API ', error);
    }
}

controller

const url = 'http://localhost:8000';
export const uploadFile = async(request, response)=>{
    if(!request.file){
        return response.status(404).json("file not found")
    }
    const imageUrl = `${url}/file/${request.file.filename}`;
    return response.status(200).json(imageUrl);
}

route.js

//Route for file upload
route.post('/file/upload', upload.single("file"), uploadFile);

error

C:\Users\user\OneDrive\Documents\webdev\whatsapp clone self\server\node_modules\multer-gridfs-storage\lib\gridfs.js:306
                        id: f._id,
                              ^

TypeError: Cannot read properties of undefined (reading '_id')       
    at GridFSBucketWriteStream.emitFile (C:\Users\Siddh\OneDrive\Documents\webdev\whatsapp clone self\server\node_modules\multer-gridfs-storage\lib\gridfs.js:306:31)
    at GridFSBucketWriteStream.emit (node:events:526:35)
    at finish (node:internal/streams/writable:807:10)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)

Node.js v20.9.0
[nodemon] app crashed - waiting for file changes before starting...

I'm working on creating a WhatsApp clone and implementing a file sharing feature. Everything was going smoothly until I encountered this error that's causing my backend to crash. The strange thing is, I can see the file in MongoDB, but the error persists. I'm hoping someone can help me troubleshoot this issue.

Here's a summary of what's happening:

I have a file sharing feature in my WhatsApp clone app. When a user tries to share a file, it's saved to MongoDB. I can confirm that the file exists in the MongoDB database. However, the backend crashes with an error that I can't seem to resolve. I'm not sure where to begin troubleshooting this issue. Can anyone provide insights on what might be causing this problem? Is there a mon mistake I might be making when dealing with file sharing in a MongoDB database?

i cant post all my code here my question will be too long [here is my github link][1]

api.js

export const uploadFile = async (data) => {
    try {
        return await axios.post(`${url}/file/upload`, data);
    } catch (error) {
        console.log('Error while calling uploadfile API ', error);
    }
}

controller

const url = 'http://localhost:8000';
export const uploadFile = async(request, response)=>{
    if(!request.file){
        return response.status(404).json("file not found")
    }
    const imageUrl = `${url}/file/${request.file.filename}`;
    return response.status(200).json(imageUrl);
}

route.js

//Route for file upload
route.post('/file/upload', upload.single("file"), uploadFile);

error

C:\Users\user\OneDrive\Documents\webdev\whatsapp clone self\server\node_modules\multer-gridfs-storage\lib\gridfs.js:306
                        id: f._id,
                              ^

TypeError: Cannot read properties of undefined (reading '_id')       
    at GridFSBucketWriteStream.emitFile (C:\Users\Siddh\OneDrive\Documents\webdev\whatsapp clone self\server\node_modules\multer-gridfs-storage\lib\gridfs.js:306:31)
    at GridFSBucketWriteStream.emit (node:events:526:35)
    at finish (node:internal/streams/writable:807:10)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)

Node.js v20.9.0
[nodemon] app crashed - waiting for file changes before starting...
Share Improve this question asked Nov 5, 2023 at 13:53 Siddhant SharmaSiddhant Sharma 631 silver badge9 bronze badges 5
  • You are sending the file data in JSON, since axios picks up the second arguement as a default JSON object. You have to select multipart/form-data. – Trishant Pahwa Commented Nov 5, 2023 at 14:02
  • Check this example request: stackoverflow./a/51587173/6072570 – Trishant Pahwa Commented Nov 5, 2023 at 14:04
  • The data is sent as multipart/form-data and is of type FormData. – Trishant Pahwa Commented Nov 5, 2023 at 14:04
  • @TrishantPahwa i tried this too ``` axios({ method : "post", url:${url}/file/upload, data : datas, headers: { 'content-type': 'multipart/form-data' } });``` i also added header but still app crashing and network tab showing network error – Siddhant Sharma Commented Nov 5, 2023 at 15:15
  • @TrishantPahwa here datas is the data function receiving from FormData – Siddhant Sharma Commented Nov 5, 2023 at 15:17
Add a ment  | 

4 Answers 4

Reset to default 3

Title: Solution for multer-gridfs error "TypeError: Cannot read properties of undefined (reading '_id')"

Answer:

It appears that you're encountering an issue with multer-gridfs that could be related to patibility with your MongoDB version. To resolve this problem, consider downgrading your MongoDB version to one that is patible with multer and multer-gridfs. Here's a step-by-step guide:

1. Uninstall MongoDB:

npm uninstall MongoDB

2. Install Compatible Version (e.g., 5.9.1):

npm install [email protected]

This should help resolve the "TypeError: Cannot read properties of undefined (reading '_id')" issue you're facing. Ensure to restart your Node.js application after making these changes.

Additionally, it's essential to check the patibility matrix of multer-gridfs with MongoDB versions. Upgrading or downgrading packages can sometimes introduce other patibility issues, so it's remended to review the documentation and release notes for both multer-gridfs and MongoDB.

If the problem persists or if you encounter any other issues during the process, feel free to provide more details or ask for further assistance.

Its seems that the multer is not patible with the latest mongodb so in your backend folder install the mongodb version 5.9.1 npm i [email protected]

for more info see https://github./devconcept/multer-gridfs-storage/issues/560

npm install [email protected]

Run this and it gets fixed instantly

I just had this same issue, however I'm not using Yarn.

To resolve I needed to install mongodb v5.9.1

npm install [email protected]

This instantly fixed the issue.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745270342a4619707.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信