Node+MongoDB+Express hangs in HTTP, but the shell works fine

express
nodejs
mongodb

#1

Workspace is public here: https://ide.c9.io/cubap/rerum

I was following a tutorial to learn how to use MongoDB in Node and everything seemed to be peachy in the shell. I can get the record I inserted into the ‘rerum.info’ collection easily.

However, when I set up app.get('/:collection'... (index.js:25), it pooped. The expectation of GETting https://rerum-cubap.c9users.io/info in the browser would be a simple table of the one record I can see easily in the shell. It appears to be successful through about collectionDriver.js:18, meaning the cursor seems to come back fine. However, if I try at that point to run anything on the cursor (like toArray()) it just spins and spins. The symptom in the browser is a 2 minute wait and a 502 error.

Until very recently, I was strictly middle and front end, so there may be something quite obvious happening and I can’t see it. Is it a simple http(s) issue? Am I barking up the wrong port or ip? Is there some gap in the callback chain so the response is never resolving?


#2

Even simple GETs are not working. I tried just a public html page and it isn’t working, so maybe the trouble is in Express before MongoDB is ever pinged.


#3

Try making your application preview public if it’s not already (via the share button in the top right). If it’s private your app won’t be able to access it’s own URL because it needs to authenticate with Cloud9 to access your application page.


#4

Thank you. It is all showing as open and public. I had included these links in the original post and I tested them to make sure I can see it when not logged in.

I am able to get the simple GETs again (like this), so it is just hanging in the Mongo access. I’m going to install q and pull the callbacks into promises to make it easier to trace and see what I can discover.


#5

I’ve got it down to one small problem that brings us full circle. I can send a result of [ { "key" : "value" } ] and everything renders just fine. The problem is definitely that cursor.toArray() just spins and spins.

In cursor.js, it gets all the way into fetchDocs() where is goes into the mongodb-core cursor.js and recognizes that the cursor is new (line 532). if(!self.topology.isConnected(self.options) && self.disconnectHandler != null) is true, but no document ever seems to be grabbed. Everything I can find is undefined.

What’s happening?


#6

Maybe I got it. The docs I was using had updated their MongoClient to use .connect(), but were still close()ing the connection. This seemed to be happening before I took the cursor back in for documents. I got rid of the closure and it works. I am certain this will cause problems down the road, but that’s what happened to me here.