Note that there are some explanatory texts on larger screens.

plurals
  1. POMongodb find() method freezes over Nodejs
    primarykey
    data
    text
    <p>I'm starting webapps development with Node, so I'm quite new with this technology.</p> <p>I've done several examples and managed to make it run.</p> <p>However, now I'm trying to use Mongodb but I'm not able to make it run properly. It seems that the connection is working but when I call the find() method and I go to localhost:3000/near (which is the url that makes the findAll docs), the browser spends about a half a minute loading and finishes returning nothing (<strong>ERR_EMPTY_RESPONSE</strong>).</p> <p>This is my code:</p> <p>app.js</p> <pre><code>app.get('/near', function(req, res){ server.findAll( function(error,docs){ res.render('near', { title: 'Cerca tuyo!', places:docs }); }) }); </code></pre> <p>And this my Server.js</p> <pre><code>//constructor AppServer = function(host, port) { this.db= new Db('node-mongo-places', new Server(host, port, {safe: false}, {auto_reconnect: true}, {})); this.db.open(function(){}); }; //Devuelve la colección de "places" AppServer.prototype.getCollection= function(callback) { this.db.collection('places', function(error, places_collection) { if( error ) callback(error); else callback(null, places_collection); }); }; //Devuelve todos los contenidos de la DB places AppServer.prototype.findAll = function(callback) { this.getCollection(function(error, places_collection) { if( error ) callback(error) else { places_collection.find().toArray(function(error, results) { if( error ) callback(error) else callback(null, results) }); } }); }; </code></pre> <p>I have debugged the code writing console.log() and it seems that it does not reach to the callback function from <code>places_collection.find().toArray(function(error, results) {</code></p> <p>Any idea why the server does not return anything?</p> <hr> <h2><strong>!!! EDIT:</strong></h2> <p>I found some piece of code to test if the connection to mongodb is alright and it says that it's not: <code>[Error: failed to connect to [localhost:27017]]</code></p> <p>So the problem is in the connection. Should I run mongodb first? I thought that the app.js itself would did that for me!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload