0

Getting node.js and gridfs to play nice is not easy. Of all the things I tried, I settled on this being as close as I could get with limited knowledge and what I understand the current supported functions will allow.

(below in coffee, use http://js2coffee.org/ to get to the js and vice versa)

util = require("util")
mongodb = require("mongodb")
GridStore = mongodb.GridStore
parse = (options) ->
  opts = {}
  opts = options[0]  if options.length > 0
  opts.metadata = {}  unless opts.metadata
  opts

db = new Db("local", new Server("127.0.0.1", 27017,
  auto_reconnect: false
  poolSize: 1
),
  native_parser: false
)
db.open()
putFile = (path, name, options, fn) ->
  options = parse(options)
  options.metadata.filename = name
  new GridStore(db, name, "w", options).open (err, file) ->
    return fn(err)  if err
    file.writeFile path, (err, fn) ->
      file.close()

opts = content_type: "plain/text"
myfileupload = putFile("myfile.txt", "known_hosts", opts)
db.close()

Strangely however, using apt-get install mongodb-10gen on Ubuntu 11.10, my file is not saved. And there are no error messages to help me understand why.

I am close to believing that everything I read about gridfs and nodejs is all just a cruel joke, and I am never going to see this work. Please help.

  • if u r having the problem of installing MongoDB, i had the same problem and see this question i asked: http://stackoverflow.com/questions/10046101/installing-and-starting-mongodb-for-php-on-ubuntu-12-04 – Arjun Bajaj Apr 10 '12 at 18:20
  • oh, no problem with install... I've done it a thousand times over. Had most fun with the ubuntu packaged version, but I found too many contradictory elements appearing, I wonder why they even bothered packaging it. I had a look at your post - and commented too ;-) – Ones and Zeroes Apr 10 '12 at 18:43

1 Answers1

2

I suspect an async issue. You're calling db.close() immediately after the call to putFile, so db.close() is running before the GridStore's open callback fires, right? Seems like it could be a problem. Try moving db.close() into the same callback as file.close().

Trevor Burnham
  • 76,828
  • 33
  • 160
  • 196
  • Thank you for pointing that out. Moving db.close() into the same callback as file.close() does make sense. I think I have another issue however, because I put the db.close() immediately following the file.close() in the same callback, but I'm still seeing it run without errors and no save. (Doing the same using C# from a remote windows client works, so its not my MongoDB that's the issue). Thanks anyway. – Ones and Zeroes Apr 10 '12 at 20:10
  • I was able to get the desired result by using a "return file.writeFile(path, fn);" as per an example I found here: github.com/jamescarr/nodejs-mongodb-streaming – Ones and Zeroes Apr 19 '12 at 08:07
  • @OnesandZeroes How did you actually get to solve this issue? I have been battling this for almost a day. Weird thing is that in my mongoose logs, i see mongoose doing finds Mongoose: fs.files.find({ filename: '81156f486d17269f524b4b313a808df7.jpg' }) { readPreference: 'primary', w: 'majority' } Mongoose: fs.chunks.find({ n: 0, files_id: ObjectId("54bb815b1b1bdbd2686f20b0") }) { readPreference: 'primary', w: 'majority' } instead of inserts as i would expect to happen since i'm writting to the DB. What do you think i am missing – shanks Jan 18 '15 at 09:58