lundi 1 février 2016

MongoDB + NodeJs simple query not working

I am trying to learn the MEAN stack, and right now I am using NodeJS + MongoDB.

In this helloworld project that I am making I have the following requirements:

  "dependencies": {
    "async": "0.9.0",
    "mongodb": "2.0.27"
  }

This project consists of making a simple insertion in the DB, and a query. In this case I want to query by the director name:

/*
*  Finds all documents in the "movies" collection
*  whose "director" field equals the given director,
*  ordered by the movie's "title" field. See
*  http://ift.tt/1PPOdRG
*/
exports.byDirector = function(db, director, callback) {
    db.collection('movies').find({director: director}).toarray(function(error, docs){
        callback(error, docs);
    });
};

However, in the tests of the tutorial, I fail, by getting the following error:

[14:27:33] Starting 'test'...
[14:27:33] Finished 'test' after 1.1 ms

  1) dbInterface can query data by director:
     TypeError: db.collection(...).find(...).sort(...).toarray is not a function
      at Object.exports.byDirector
Tests failed!

I suppose I am having some kind of syntax error, but I honestly can't find out what it is.

What am I missing?

Aucun commentaire:

Enregistrer un commentaire