Closed 360disrupt closed 5 years ago
The two cases writing a new file and updating the metadata are pretty straight forward. But what's the way to update the file buffer.
const fileBuffer = file.buffer; delete file.buffer; const readStream = new Readable(); readStream.push(fileBuffer); readStream.push(null);
Writing a new file:
Attachment.write(file, readStream, callback)
But as soon as the _id already is in the DB it throws an err. My current approach is to unlink the file first but it looks smelly.
Attachment.unlink({_id: file._id}, (err) => { if (err) {return callback(err)} Attachment.write(file, readStream, callback) });
I know I can update the metadata with Attachment.updateOne but is there a solution to update the actual file buffer?
Attachment.updateOne
Didn't find anything here regarding updates: https://docs.mongodb.com/manual/core/gridfs/
@360disrupt I may say the only way to update the file would be creating new one.
How:
_id
The two cases writing a new file and updating the metadata are pretty straight forward. But what's the way to update the file buffer.
Writing a new file:
Attachment.write(file, readStream, callback)
But as soon as the _id already is in the DB it throws an err. My current approach is to unlink the file first but it looks smelly.
I know I can update the metadata with
Attachment.updateOne
but is there a solution to update the actual file buffer?Didn't find anything here regarding updates: https://docs.mongodb.com/manual/core/gridfs/