Completely at a loss for the Basic Joins exercise

I have watched the video for this lesson about 5 times, on a big screen and in full screen mode.

When I do my Aggregation as per the video in Compass, then it works OK

If I try to copy the code, or if I type it in manually, I am falling at the first test with error:

Get Comments
_ × Can fetch comments for a movie (99ms)_
_ × comments should be sorted by date (87ms)_
_ ● Get Comments › Can fetch comments for a movie_
_ TypeError: Cannot read property ‘length’ of undefined_

I have no clue how to fix this, so need to get some help

Please advise what to do, as I am running out of time!!

thanks

Please provide error messages. The test summaries are not enough. However the TypeError (‘length’ undefined) means you do not return a movie with an array of comments.

Thanks for the response, here is the full messages from the test

> jest --passWithNoTests “get-comments”

Determining test suites to run…Setup Mongo Connection
_ FAIL test/get-comments.test.js_
_ Get Comments_
_ × Can fetch comments for a movie (99ms)_
_ × comments should be sorted by date (87ms)_

_ ● Get Comments › Can fetch comments for a movie_

_ TypeError: Cannot read property ‘length’ of undefined_

_ 10 | const movie = await MoviesDAO.getMovieByID(id)_
_ 11 | expect(movie.title).toEqual(“Once Upon a Time in Mexico”)_
_ > 12 | expect(movie.comments.length).toBe(2)_
_ | ^_
_ 13 | })_
_ 14 |_
_ 15 | test(“comments should be sorted by date”, async () => {_

_ at length (test/get-comments.test.js:12:27)_
_ at tryCatch (node_modules/regenerator-runtime/runtime.js:62:40)_
_ at Generator.invoke [as invoke] (node_modules/regenerator-runtime/runtime.js:296:22)
_ at Generator.prototype.(anonymous function) [as next] (node_modules/regenerator-runtime/runtime.js:114:21)_
_ at step (node_modules/babel-runtime/helpers/asyncToGenerator.js:17:30)_
_ at node_modules/babel-runtime/helpers/asyncToGenerator.js:28:13_

_ ● Get Comments › comments should be sorted by date_

_ TypeError: Cannot read property ‘slice’ of undefined_

_ 30 | const movie = await MoviesDAO.getMovieByID(id)_
_ 31 | const comments = movie.comments_
_ > 32 | const sortedComments = comments.slice()_
_ | ^_
_ 33 | sortedComments.sort((a, b) => b.date.getTime() - a.date.getTime())_
_ 34 |_
_ 35 | for (let i = 0; i < Math.min(10, comments.length); i++) {_

_ at slice (test/get-comments.test.js:32:39)_
_ at tryCatch (node_modules/regenerator-runtime/runtime.js:62:40)_
_ at Generator.invoke [as invoke] (node_modules/regenerator-runtime/runtime.js:296:22)
_ at Generator.prototype.(anonymous function) [as next] (node_modules/regenerator-runtime/runtime.js:114:21)_
_ at step (node_modules/babel-runtime/helpers/asyncToGenerator.js:17:30)_
_ at node_modules/babel-runtime/helpers/asyncToGenerator.js:28:13_

_ ● Get Comments › comments should be sorted by date_

_ expect.assertions(24)_

_ Expected 24 assertions to be called but received zero assertion calls._

_ 15 | test(“comments should be sorted by date”, async () => {_
_ 16 | // most recent to least_
_ > 17 | expect.assertions(24)_
_ | ^_
_ 18 | const movieIds = [_
_ 19 | “573a1391f29313caabcd8414”,_
_ 20 | “573a1391f29313caabcd9058”,_

_ at assertions (test/get-comments.test.js:17:12)_
_ at tryCatch (node_modules/regenerator-runtime/runtime.js:62:40)_
_ at Generator.invoke [as invoke] (node_modules/regenerator-runtime/runtime.js:296:22)
_ at Generator.prototype.(anonymous function) [as next] (node_modules/regenerator-runtime/runtime.js:114:21)_
_ at step (node_modules/babel-runtime/helpers/asyncToGenerator.js:17:30)_
_ at node_modules/babel-runtime/helpers/asyncToGenerator.js:35:14_
_ at new F (node_modules/babel-runtime/node_modules/core-js/library/modules/export.js:36:28)
_ at Object. (node_modules/babel-runtime/helpers/asyncToGenerator.js:14:12)_

Test Suites: 1 failed, 1 total
Tests: 2 failed, 2 total

Your getMovieByID() does not returned the comments as required. There is a thread for that:

https://discourse.university.mongodb.com/t/create-update-delete-comments/15640/35

I don’t really get where I’m going wrong, so after reading the thread you point me to, which seems to tell me to wrap movie_id in ObjectId() I get the following errors, depending on what bits I wrap :slight_smile:

Something went wrong in getMovieByID: ReferenceError: movie_id is not defined
Something went wrong in getMovieByID: ReferenceError: $movie_id is not defined
Something went wrong in getMovieByID: Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters

1 Like

OK, I got it now

thanks

const pipeline = [{ $match: { _id: ObjectId(id) } },
{
$lookup: {
‘from’: ‘comments’,
‘let’: { ‘id’: ‘_id’ },
‘pipeline’: [{ ‘$match’: { ‘$expr’: { ‘$eq’: [’$movie_id’, ‘$$id’] } } },
{ “$sort”: { “date”: -1 }, }], as: ‘comments’
}
}]
getmoviebyId is not working as expected please help me in sortingout this issue

1 Like

There’s an issue here:

let’: { ‘id’: ‘_id’ }

When we need to refer to a field, we have to use the $ prefix. When referring to a user defined variable, we use two $ as you did in your $match.

1 Like

const pipeline = [{ $match: { _id: ObjectId(id) } },
{
lookup: { 'from': 'comments', 'let': { 'id': '(_id)’ },
‘pipeline’: [{ ‘$match’: { ‘$expr’: { ‘$eq’: [’$movie_id’, ‘$$id’] } } },
{ “$sort”: { “date”: -1 }, }], as: ‘comments’
}
}]
updated to S(_id)still same issue

Please see my response here. Ticket Get Comments

1 Like