Javascript.RU

Создать новую тему Ответ
 
Опции темы Искать в теме
  #1 (permalink)  
Старый 15.06.2017, 13:30
Интересующийся
Отправить личное сообщение для ASK9292 Посмотреть профиль Найти все сообщения от ASK9292
 
Регистрация: 19.09.2016
Сообщений: 15

Работа с 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
Ответить с цитированием
  #2 (permalink)  
Старый 15.06.2017, 13:51
Профессор
Отправить личное сообщение для Audaxviator Посмотреть профиль Найти все сообщения от Audaxviator
 
Регистрация: 28.04.2017
Сообщений: 214

А какой режиссёр-то запрашивается?
Ответить с цитированием
  #3 (permalink)  
Старый 15.06.2017, 14:19
Интересующийся
Отправить личное сообщение для ASK9292 Посмотреть профиль Найти все сообщения от ASK9292
 
Регистрация: 19.09.2016
Сообщений: 15

Сообщение от Audaxviator Посмотреть сообщение
А какой режиссёр-то запрашивается?
Запрос идет на названия фильмов, режисер - фильм
Ответить с цитированием
  #4 (permalink)  
Старый 15.06.2017, 14:27
Профессор
Отправить личное сообщение для Audaxviator Посмотреть профиль Найти все сообщения от Audaxviator
 
Регистрация: 28.04.2017
Сообщений: 214

Я в коде вижу - find({ director }) - какой? Может быть, допустим, find({ director: 'Irvin Kershner' }) - а так не понятно, что find?
Ответить с цитированием
  #5 (permalink)  
Старый 15.06.2017, 14:38
Интересующийся
Отправить личное сообщение для ASK9292 Посмотреть профиль Найти все сообщения от ASK9292
 
Регистрация: 19.09.2016
Сообщений: 15

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!
Ответить с цитированием
  #6 (permalink)  
Старый 15.06.2017, 14:42
Профессор
Отправить личное сообщение для Audaxviator Посмотреть профиль Найти все сообщения от Audaxviator
 
Регистрация: 28.04.2017
Сообщений: 214

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

Вообще, синтаксис такой - collection('movies') - я не видел. Я не авторитет, конечно, но - не видел. И toArray - тоже.
Ответить с цитированием
  #8 (permalink)  
Старый 15.06.2017, 14:49
Интересующийся
Отправить личное сообщение для ASK9292 Посмотреть профиль Найти все сообщения от ASK9292
 
Регистрация: 19.09.2016
Сообщений: 15

Не удается прочитать свойство 'метод exec' не определен
Ответить с цитированием
  #9 (permalink)  
Старый 15.06.2017, 14:52
Профессор
Отправить личное сообщение для Audaxviator Посмотреть профиль Найти все сообщения от Audaxviator
 
Регистрация: 28.04.2017
Сообщений: 214

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

И всё, кажется.
Ответить с цитированием
  #10 (permalink)  
Старый 15.06.2017, 15:01
Интересующийся
Отправить личное сообщение для ASK9292 Посмотреть профиль Найти все сообщения от ASK9292
 
Регистрация: 19.09.2016
Сообщений: 15

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!
Ответить с цитированием
Ответ



Опции темы Искать в теме
Искать в теме:

Расширенный поиск


Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
работа с json файлом forwardin Общие вопросы Javascript 5 28.03.2017 21:46
JSON - работа с данными Зосимов Общие вопросы Javascript 9 31.01.2014 12:15
Json ответ, работа с data SergeyMiracle AJAX и COMET 7 03.07.2013 19:29
Работа с json mrozk Общие вопросы Javascript 3 03.08.2012 01:00
Разная работа скрипта с JSON на разных хостингах Rokugan Общие вопросы Javascript 1 03.10.2011 13:16