Problem with MongoDB GridFS Saving Files with Node.JS

后端 未结 1 366
-上瘾入骨i
-上瘾入骨i 2020-12-10 09:40

I have a function saving a file to gridfs. It somehow stopped working sporadically after a refactor and I\'ve spent over 2 hours staring blankly at it. I swear it\'s rough

相关标签:
1条回答
  • 2020-12-10 10:20

    There are a couple of solutions. You can use writeBuffer, writeFile or the new simple grid class. Under is your example adjusted for the fact of using a buffer instance.

    // You can use an object id as well as filename now
    var gs = new mongodb.GridStore(this.db, filename, "w", {
      "chunk_size": 1024*4,
      metadata: {
        hashpath:gridfs_name,
        hash:hash,
        name: name
      }
    });
    
    gs.open(function(err,store) {
      // Write data and automatically close on finished write
      gs.writeBuffer(data, true, function(err,chunk) {
        // Each file has an md5 in the file structure
        cb(err,hash,chunk);
      });
    });
    

    In general the best place to start are the tests that cover a wide usage profile for the gridfs classes. Look at.

    https://github.com/christkv/node-mongodb-native/tree/master/test/gridstore

    0 讨论(0)
提交回复
热议问题