title: get collection data in nodejs and mongodb
body: <p>Tell me please, how can I create REST API using backbone application on the client and nodejs + mongodb in the server side.</p>
  
  <p>I am new in nodejs and can't understand all of the elementary things.</p>
  
  <p>Just for example — I need to get friends collection.
  In my client app I say</p>
  
  <pre><code>var collection = new Backbone.Collection.exted({ 
    model: model,
    url: '/api/friends'
  });
  collection.fetch();
  </code></pre>
  
  <p>Ok, on the server (nodejs + express) I can listen this request with 
  app.get('/api/friends', friends.get);</p>
  
  <p>in the «friends» module I have «get» function. It connect to database, try to get data from «friends» collection (if collection is not exist it must be create). If collection is empty, function must to initialize request to the vk.com (social netword) server for its data.</p>
  
  <pre><code>/*
   * GET friends
   */
  var https = require('https'),
    Db = require('mongodb').Db,
    Server = require('mongodb').Server;
  
  var client = new Db('data', new Server('127.0.0.1', 27017, {})),
    friends;
  
  function getAllFriends(err, collection) {
    if (!collection.stats()) {
      https.get('https://api.vk.com/method/friends.get?access_token=' + global['access_token'], function (d) {
        var chunk = '',
          response;
        d.on('data', function (data) {
          chunk += data;
        });
  
        d.on('end', function () {
          response = JSON.parse(chunk).response;
          response.forEach(function (friend) {
            collection.insert(friend, function (err, docs) {
              if (err) console.log(err);
            });
          });
        }).on('error', function (e) {
          console.error(e);
        });
      });
    }
  
    friends = collection.find({}, {
      limit: 1000
    }).toArray(function (err, docs) {
      return docs;
    });
  }
  
  exports.get = function (req, res) {
    client.open(function (err, pClient) {
      client.collection('friends', getAllFriends);
      res.send(friends);
    });
  };
  </code></pre>
  
  <p>But this code don't work. I have no idea why.</p>
  
  <pre><code>/node_modules/mongodb/lib/mongodb/connection/server.js:524
          throw err;
                ^
  TypeError: Cannot read property 'noReturn' of undefined
      at Cursor.nextObject.commandHandler (/node_modules/mongodb/lib/mongodb/cursor.js:623:17)
      at Db._executeQueryCommand (/node_modules/mongodb/lib/mongodb/db.js:1702:5)
      at g (events.js:192:14)
      at EventEmitter.emit (events.js:126:20)
      at Server.Base._callHandler (/node_modules/mongodb/lib/mongodb/connection/base.js:130:25)
      at Server.connect.connectionPool.on.server._serverState (/node_modules/mongodb/lib/mongodb/connection/server.js:517:20)
      at MongoReply.parseBody (/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:127:5)
      at Server.connect.connectionPool.on.server._serverState (/node_modules/mongodb/lib/mongodb/connection/server.js:476:22)
      at EventEmitter.emit (events.js:96:17)
      at _connect (/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:175:13)
  </code></pre>
  
  <p>Maybe this code is not creating collection when it needs?</p>
  
  <p>Maybe I do wrong anything, so can you give me a good link to read tutorial in nodejs or mongo.</p>
  
  <p>Thanks for your advice and sorry please for my english.</p>