AKM
AKM

Reputation: 31

How to save the ID of a saved file using GridFS?

I am trying to save the ID of the file that I send via GridFS to my MongoDB (working with Mongoose). However I cant seem to find out how to get the created ID in fs.files with code?

var writestream = gfs.createWriteStream({
    filename: req.file.originalname
});

fs.createReadStream(req.file.path).pipe(writestream);

writestream.on('close', function (file) {
    // do something with `file`
    console.log(file.filename + 'Written To DB');
});

I cant seem to be able to save the ID of the File that I wrote via the writestream.

The file is created in the lists etc. but how do I manage to save the file so that I can save it in one of my other MongoDB Documents?

Upvotes: 2

Views: 1144

Answers (1)

Etienne
Etienne

Reputation: 21

This is old and I think you already solved you problem. If I correctly understood, you were trying to get the id of the saved file. You can simply do this:

console.log(writestream.id);

Upvotes: 1

Related Questions