Javascript-форум (https://javascript.ru/forum/)
-   Node.JS (https://javascript.ru/forum/node-js-io-js/)
-   -   Работа с JSON (https://javascript.ru/forum/node-js-io-js/69332-rabota-s-json.html)

ASK9292 15.06.2017 13:30

Работа с JSON
 
Учу Ноду,

Вот ТЗ: Please download attachments for this homework here

For this section's assessment, you will fill out the functions defined in interface.js. These functions will query a collection of movies - the data in the movies collection will look like what you see in the movies.json file.

In order to complete this section's assessment, you will have to make the tests in test.js pass. To run the tests, run npm install and then npm test. You will notice the tests fail. In keeping with this section's emphasis on automation, we recommend you use npm run watch to re-run your tests every time you change the interface.js file. Once the tests succeed, npm run watch will terminate and print a "secret code" consisting of a short phrase. Copy this code into the assessment page in your browser to complete this assessment.

Как уже понятно работа в связке Node+Mongo+npm

так вот, пишу этот файл:

/*
 *  Inserts "doc" into the collection "movies".
 */
exports.insert = function(db, doc, callback) {
	db.collection('movies').insert(doc);
  callback(null);
};

/*
 *  Finds all documents in the "movies" collection
 *  whose "director" field equals the given director,
 *  ordered by the movie's "title" field. See
 *  [url]http://mongodb.github.io/node-mongodb-native/2.0/api/Cursor.html#sort[/url]
 */
exports.byDirector = function(db, director, callback) {
	db.collection('movies').find({ director }).toArray(function(error, docs) {
  console.log(docs);
});
  callback(null, []);
};


ошибку при запуске тестов выкидывает следущее

:~/Documents/home_work$ npm test

> @ test /home/ask/Documents/home_work
> gulp test

[13:23:21] Using gulpfile ~/Documents/home_work/gulpfile.js
[13:23:21] Starting 'test'...
[13:23:21] Finished 'test' after 11 ms


dbInterface
✓ can insert a movie
1) can query data by director
[ { _id: 5942601906cddd10228df69c,
title: 'The Empire Strikes Back',
year: 1980,
director: 'Irvin Kershner' } ]
2) returns multiple results ordered by title
[ { _id: 5942601906cddd10228df69b,
title: 'Star Wars',
year: 1977,
director: 'George Lucas' },
{ _id: 5942601906cddd10228df69e,
title: 'The Phantom Menace',
year: 1999,
director: 'George Lucas' },
{ _id: 5942601906cddd10228df69f,
title: 'Attack of the Clones',
year: 2002,
director: 'George Lucas' },
{ _id: 5942601906cddd10228df6a0,
title: 'Revenge of the Sith',
year: 2005,
director: 'George Lucas' } ]


1 passing (292ms)
2 failing

1) dbInterface can query data by director:

AssertionError: 0 == 1
+ expected - actual

+1
-0

at test.js:41:14
at Object.exports.byDirector (interface.js:19:3)
at Context.<anonymous> (test.js:38:17)

2) dbInterface returns multiple results ordered by title:

AssertionError: 0 == 4
+ expected - actual

+4
-0

at test.js:57:14
at Object.exports.byDirector (interface.js:19:3)
at Context.<anonymous> (test.js:54:17)



Tests failed!

Объясните пожалуйста, что я делаю не так, в поиске фильмов по JSON

Audaxviator 15.06.2017 13:51

А какой режиссёр-то запрашивается?

ASK9292 15.06.2017 14:19

Цитата:

Сообщение от Audaxviator (Сообщение 455536)
А какой режиссёр-то запрашивается?

Запрос идет на названия фильмов, режисер - фильм

Audaxviator 15.06.2017 14:27

Я в коде вижу - find({ director }) - какой? Может быть, допустим, find({ director: 'Irvin Kershner' }) - а так не понятно, что find?

ASK9292 15.06.2017 14:38

b.collection('movies').find({ director: 'George Lucas' }).toArray(function(error, docs)


~/Documents/home_work$ npm test

> @ test /home/ask/Documents/home_work
> gulp test

[14:39:05] Using gulpfile ~/Documents/home_work/gulpfile.js
[14:39:05] Starting 'test'...
[14:39:05] Finished 'test' after 16 ms


  dbInterface
    ✓ can insert a movie
    1) can query data by director
[ { _id: 594271da2bc194af2def0d07,
    title: 'Star Wars',
    year: 1977,
    director: 'George Lucas' },
  { _id: 594271da2bc194af2def0d0a,
    title: 'The Phantom Menace',
    year: 1999,
    director: 'George Lucas' },
  { _id: 594271da2bc194af2def0d0b,
    title: 'Attack of the Clones',
    year: 2002,
    director: 'George Lucas' },
  { _id: 594271da2bc194af2def0d0c,
    title: 'Revenge of the Sith',
    year: 2005,
    director: 'George Lucas' } ]
    2) returns multiple results ordered by title
[ { _id: 594271da2bc194af2def0d07,
    title: 'Star Wars',
    year: 1977,
    director: 'George Lucas' },
  { _id: 594271da2bc194af2def0d0a,
    title: 'The Phantom Menace',
    year: 1999,
    director: 'George Lucas' },
  { _id: 594271da2bc194af2def0d0b,
    title: 'Attack of the Clones',
    year: 2002,
    director: 'George Lucas' },
  { _id: 594271da2bc194af2def0d0c,
    title: 'Revenge of the Sith',
    year: 2005,
    director: 'George Lucas' } ]


  1 passing (338ms)
  2 failing

  1) dbInterface can query data by director:

      AssertionError: 0 == 1
      + expected - actual

      +1
      -0
      
      at test.js:41:14
      at Object.exports.byDirector (interface.js:19:3)
      at Context.<anonymous> (test.js:38:17)

  2) dbInterface returns multiple results ordered by title:

      AssertionError: 0 == 4
      + expected - actual

      +4
      -0
      
      at test.js:57:14
      at Object.exports.byDirector (interface.js:19:3)
      at Context.<anonymous> (test.js:54:17)



Tests failed!

Audaxviator 15.06.2017 14:42

И потом, это нативный драйвер MongoDB что ли? Если я правильно помню, у него функция поиска возвращает объект curcor. То есть результат нужно передать в переменную - типа
var cursor = db.movies.find({director: 'Irvin Kershner'});
- и потом с ней уже что-то делать. А в Mongoose вместо toArray(... нужно было бы написать exec(... Короче, ту про драйвер не понятно.

Audaxviator 15.06.2017 14:45

Вообще, синтаксис такой - collection('movies') - я не видел. Я не авторитет, конечно, но - не видел. И toArray - тоже.

ASK9292 15.06.2017 14:49

Не удается прочитать свойство 'метод exec' не определен

Audaxviator 15.06.2017 14:52

Нет-нет, exec - это в драйвере Mongoose, я же говорю. А тут просто попробуйте так написать:
var cursor = db.movies.find({director: 'Irvin Kershner'});
console.log(cursor);

И всё, кажется.

ASK9292 15.06.2017 15:01

var cursor = db.movies.find({director: 'Irvin Kershner'});
console.log(cursor);


dbInterface
✓ can insert a movie
1) can query data by director
2) returns multiple results ordered by title


1 passing (265ms)
2 failing

1) dbInterface can query data by director:
TypeError: Cannot read property 'find' of undefined
at Object.exports.byDirector (interface.js:16:24)
at Context.<anonymous> (test.js:38:17)

2) dbInterface returns multiple results ordered by title:
TypeError: Cannot read property 'find' of undefined
at Object.exports.byDirector (interface.js:16:24)
at Context.<anonymous> (test.js:54:17)



Tests failed!


Часовой пояс GMT +3, время: 12:17.