最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Deleting file by metadata with gridfs in NodeJS - Stack Overflow

programmeradmin2浏览0评论

I am trying to delete files in my mongodb database using gridfs. I want to delete all the files with the metadata.relation = id. Here is my method in NodeJS:

function deleteFiles(){
    gfs.remove({'metadata.relation': req.body._id }, function(err){
      if (err) return false;
      return true;          
    })
}

The error is:

C:\Users\Gaute\Documents\GitHub\WikiHelpSystem\node_modules\mongoose\node_module s\mongodb\lib\mongodb\gridfs\gridstore.js:1138
if(names.constructor == Array) {

     ^

TypeError: Cannot read property 'constructor' of undefined at Function.GridStore.unlink (C:\Users\Gaute\Documents\GitHub\WikiHelpSystem \node_modules\mongoose\node_modules\mongodb\lib\mongodb\gridfs\gridstore.js:1138 :11)

I am trying to delete files in my mongodb database using gridfs. I want to delete all the files with the metadata.relation = id. Here is my method in NodeJS:

function deleteFiles(){
    gfs.remove({'metadata.relation': req.body._id }, function(err){
      if (err) return false;
      return true;          
    })
}

The error is:

C:\Users\Gaute\Documents\GitHub\WikiHelpSystem\node_modules\mongoose\node_module s\mongodb\lib\mongodb\gridfs\gridstore.js:1138
if(names.constructor == Array) {

     ^

TypeError: Cannot read property 'constructor' of undefined at Function.GridStore.unlink (C:\Users\Gaute\Documents\GitHub\WikiHelpSystem \node_modules\mongoose\node_modules\mongodb\lib\mongodb\gridfs\gridstore.js:1138 :11)

Share Improve this question edited May 10, 2014 at 0:12 Neil Lunn 151k36 gold badges355 silver badges325 bronze badges asked May 7, 2014 at 10:02 user2925894user2925894 2592 gold badges5 silver badges17 bronze badges 3
  • you are checking whether names is an array or not, but it says it is undefined. What is names? Put some code snippets – anvarik Commented May 7, 2014 at 10:56
  • I am not checking this. Its from gridstore.js, a imported file GridStore.unlink = function(db, names, options, callback) { var self = this; var args = Array.prototype.slice.call(arguments, 2); callback = args.pop(); options = args.length ? args.shift() : null; if(names.constructor == Array) { – user2925894 Commented May 7, 2014 at 12:10
  • Can you update the post instead of pasting code in ments? You need to track names var back through the stack to figure out why it's null. – bryanmac Commented May 19, 2014 at 10:36
Add a ment  | 

2 Answers 2

Reset to default 6 +50

Assuming you are using gridfs-stream module, then when you call gfs.remove with an object, it will expect that the object will contain an _id.

You need to first get the id, using MongoDb driver.

// This should be your files metadata collection, fs.files is the default collection for it. 
var collection = db.collection('fs.files');     
collection.findOne({'metadata.relation': req.body._id }, { _id : 1 }, function (err, obj) {
    if (err) return cb(err); // don't forget to handle error/
    gfs.remove(obj, function(err){
      if (err) return false;
      return true;          
    })
});
 gfs.files.remove({_id:_id} , function(err){
                                            if (err){
                                                 console.log("error");
                                            }else{
                                              console.log("old file removed")
                                            }           
                                   }); 
发布评论

评论列表(0)

  1. 暂无评论