Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Trying to create a document and add an image to mongoDB at same time and only one gets added.
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Steven Headley  
View profile  
 More options Jun 18 2012, 12:37 pm
From: Steven Headley <steven.head...@gmail.com>
Date: Mon, 18 Jun 2012 09:37:31 -0700 (PDT)
Local: Mon, Jun 18 2012 12:37 pm
Subject: Trying to create a document and add an image to mongoDB at same time and only one gets added.

Hello all,
    I am trying to create a document while adding an image with the
following code:

exports.image_postJson =  function(req, res){
console.log('>>>>image_postJson');
// Get DB connection from MongoHQ
db = mongoose.connection.db

//Create a Image model object
image = new imageModel();
image.categoryGroup = req.body.categoryGroup;
image.categoryChild = req.body.categoryChild;
image.latitude = req.body.latitude;
image.longitude = req.body.longitude;

// GET image string
imageString = req.body.image;
console.log('imageString = '+imageString);

var dataBuffer = new Buffer(imageString, 'base64');

require("fs").writeFile("/tmp/temp.png", dataBuffer, function(err) {
  console.log(err);

});

// Our file ID from MongoHQ
var fileId = new mongoose.mongo.ObjectID();
console.log('fileId = '+fileId);

image.filename = fileId;
var gs = new mongoose.mongo.GridStore(db, fileId, 'w', {
'content_type': 'image/png',
'metadata':{
             'author': 'Steven'

},

'chunk_size': 1024*28

});

console.log('GS = '+gs);

// Open the new file
        gs.open(function(err, gridStore) {
                console.log('GS Open Error= '+err);
                if(!err) {
        console.log('no Error = '+gridStore);
        gridStore.writeFile('/tmp/temp.png', function(err, doc) {
        if(!err) {
            console.log('Success!!!'+doc.uploadDate);

}

else {
console.log('Error1 = '+err);
}
});
}

else {
console.log('Error2 = '+err);
}
});

image.save(function (err) {
        messages = [];
        errors = [];
        if (!err){
            console.log('Success!');
            messages.push("Thank you for your new image !");
        }
        else {
            console.log('Error !');
            errors.push("At least a mandatory field has not passed
validation...");
            console.log(err);
        }
console.log("success");
      });
 db.close();
mongoose.disconnect();
        res.writeHead(200, {"Content-Type": "text/html"});
    res.end();

};

If I comment out the adding of the document the images is added and if I
comment out save the image the document is created. Why won't both get
persisted? Any help would be appreciated. BTW my hosting company is MongoHQ.

Regards,

Steven H.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Aaron Heckmann  
View profile  
 More options Jun 20 2012, 1:46 pm
From: Aaron Heckmann <aaron.heckm...@gmail.com>
Date: Wed, 20 Jun 2012 10:46:40 -0700 (PDT)
Local: Wed, Jun 20 2012 1:46 pm
Subject: Re: Trying to create a document and add an image to mongoDB at same time and only one gets added.

Looks like you are closing the db connection too early:

    db.close();
    mongoose.disconnect();

Best practice for mongodb is to open the connection at application start up
and keep it open until you need to shut your _entire app_ down.

Also, you may want to take a peek at gridform<http://aheckmann.github.com/gridform/>which allows you to stream uploaded files straight to GridFS, bypassing the
filesystem completely.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »