Question

I am trying to save project and its file in GridFS. I want to save project first and using "_id" of project as metadata for file I want to save file. When i tried so i am getting ENOENT, open '/tmp/45e85388793de' error. here is my code

newProject.save(function (err,project) {
            if (err) {
              console.log('save error', err);
            }
             console.log("project added");
             var id=poject._id;
             var filepath    = req.files.file.path;
             var filename    = req.files.file.name;
           var writestream = gfs.createWriteStream({ filename: filename, metadata:id });
             console.log(filepath);                                                       
             fs.createReadStream(filepath)
             .on('end', function() {

             })
            .on('error', function(err) {
             console.log("error encountered"+err);//ENOENT,open error  
             })
            .pipe(writestream);
            });

Why i am getting this error and how to resolve it?

Was it helpful?

Solution 2

I think you are getting this error since :

Your file is saved in a temporary location. When you are inside the callback function your file is removed from that location and you are getting "No such file" error. Path and other variables still exists as part of js and that's why you are able to print them in console.

Solution: Above(Outside) callback function move your file to some other permanent location using:

        fs.rename(req.files.file.path, "./someKnownPath/filename");

Keep note of that location. In your callback function use the new location as path and try saving the file in gridfs. Once the file is saved you may delete it file from that location(/someKnownPath/filename).

OTHER TIPS

ENOENT in this context means "No such file or directory." It means the filepath you are trying to read with createReadStream does not exist.

This error was occuring for me as well. And the reason was temp directory was not in place. After I created manually and gave a try, it worked.

Now I have shifted to creating directory on the fly through node.js itself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top