aheckmann / gridform

Stream formidable uploads into MongoDB GridFS
http://aheckmann.github.com/gridform/
MIT License
59 stars 17 forks source link

Document doesn't exist when parse callback fires #1

Closed nrw closed 11 years ago

nrw commented 12 years ago

In the form.parse callback, the new file's document is only available after a delay. I don't think this the intended behavior. Am I misusing gridform?

// Requires omitted. I don't think they're the
// problem as every other operation using
// node-mongo-native behaves as expected

app.post('/', function(req, res) {
  var form = gridform({
    db: db,
    mongo: mongo
  });

  form.parse(req, function(err, fields, files) {
    var file = files.upload;

    // version 1: this works
    setTimeout(function() {
      db.collection('fs.files', function(err, coll) {
        coll.findOne({_id: file.id}, function(err, doc) {
          // doc exists
        });
      });
    }, 30);

    // version 2: this doesn't work
    db.collection('fs.files', function(err, coll) {
      coll.findOne({_id: file.id}, function(err, doc) {
        // doc is null
      });
    });

  });
});
aheckmann commented 12 years ago

not misusing. we'll need to review if we're listening to the proper event to trigger completion.

On Jul 7, 2012, at 2:59 AM, Nicholas Westlakereply@reply.github.com wrote:

In the form.parse callback, the new file's document is only available after a delay. I don't think this the intended behavior. Am I misusing gridform?

// Requires omitted. I don't think they're the
// problem as every other operation using
// node-mongo-native behaves as expected

app.post('/', function(req, res) {
 var form = gridform({
   db: db,
   mongo: mongo
 });

 form.parse(req, function(err, fields, files) {
   var file = files.upload;

   // version 1: this works
   setTimeout(function() {
     db.collection('fs.files', function(err, coll) {
       coll.findOne({_id: file.id}, function(err, doc) {
         // doc exists
       });
     });
   }, 30);

   // version 2: this doesn't work
   db.collection('fs.files', function(err, coll) {
     coll.findOne({_id: file.id}, function(err, doc) {
       // doc is null
     });
   });

 });
});

Reply to this email directly or view it on GitHub: https://github.com/aheckmann/gridform/issues/1

nrw commented 12 years ago

it looks like the write stream end event is the trigger. is there a close event we can wait for?

aheckmann commented 11 years ago

fixed in 0.1.2