On Monday, June 18, 2012 9:37:31 AM UTC-7, Steven Headley wrote:
> 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.