And intuitively tried something like Files.remove(file, function(err, file) {} but nothing goes on. I believe cfs:filesystem should have implemented the remove functionality, such as remove record from database, and delete the file on server.
So would anyone give me some examples on how to implement remove of a file? Thanks a lot!
It should work like you mentioned, as long as your Files collection is an FS.Collection. Your document structure doesn’t look like a FS.Collection document, so are you storing your files in a seperate collection? If so you’re not using it correctly.
You should insert into an FS.Collection and put metadata in there, not store additional data in a separate collection. The main thing is that you don’t store the url itself, you let collectionfs do that for you with the .url() method.
Exactly, if you remove the FS.file, collectionfs will remove the file from the server.
It seems you’re working on someone else his code, so you might not be able to change too much, but I would advise you to at least include the id to the FS.file instance in the other collection, that would make things a lot simpler to reference, like:
_id: "5AqaDtx5baiLTssJX",
canRemove: true,
createdAt: Fri Apr 24 2015 16:33:40 GMT+0800 (CST),
isPrivate: false,
name: "EL.jpg",
url: "http://localhost:3000/uploads/files-WBLnuxBJazJ23dFzK-EL.jpg",
fileID: "the ObjectID of the FS.file"
If findOne returns undefined then it’s because it can’t find the record with the query you gave (all collections have the findOne() function), this could be a problem with numerous things.
Most probable are that cfs_id isn’t the correct one, or you simply don’t have that file in your collection at that moment (publish/subscribe hasn’t returned the data or something along those lines).
This will probably be the same problem that’s causing not all files being deleted as I personally have not encountered issues with removing on collectionfs.