Unexpected reserved word 'let'

Hello.

Trying to complete Ticket: Get Comments.
I think I have the correct code:

const pipeline = [

        {

          $match: {

            _id: ObjectId(id),

          },

        },

        {

          $lookup: {

            from: "comments",

            let: { id: "$_id" },

            pipeline: [

              { $match: { $expr: { $eq: ["$movie_id", "$$id"] } } },

              { $sort: { date: -1 } },

            ],

            as: "comments",

          },

        },

      ]

And I’m able to pass the test with this code.
But I can’t run the server because I get an error saying I cant use let because it’s a javascript reserved word.

Unexpected reserved word 'let' (236:4)

  234 |  {
  235 |     from: 'comments';
> 236 |     let: {'id', '$_id'};
      |     ^
  237 |     pipeline: [
  238 |       { '$match':
  239 |         {'$expr': {'$eq': ['$movie_id', '$$_id'] } }
    at Parser.raise (C:\Users\nizan\Desktop\mflix-js\node_modules\@babel\parser\lib\index.js:6325:17)
    at Parser.checkReservedWord (C:\Users\nizan\Desktop\mflix-js\node_modules\@babel\parser\lib\index.js:9556:12)
    at Parser.parseIdentifierName (C:\Users\nizan\Desktop\mflix-js\node_modules\@babel\parser\lib\index.js:9525:12)
    at Parser.parseIdentifier (C:\Users\nizan\Desktop\mflix-js\node_modules\@babel\parser\lib\index.js:9499:23)
    at Parser.parseExprAtom (C:\Users\nizan\Desktop\mflix-js\node_modules\@babel\parser\lib\index.js:8701:27)
    at Parser.parseExprSubscripts (C:\Users\nizan\Desktop\mflix-js\node_modules\@babel\parser\lib\index.js:8412:23)
    at Parser.parseMaybeUnary (C:\Users\nizan\Desktop\mflix-js\node_modules\@babel\parser\lib\index.js:8392:21)
    at Parser.parseExprOps (C:\Users\nizan\Desktop\mflix-js\node_modules\@babel\parser\lib\index.js:8267:23)
    at Parser.parseMaybeConditional (C:\Users\nizan\Desktop\mflix-js\node_modules\@babel\parser\lib\index.js:8240:23)
    at Parser.parseMaybeAssign (C:\Users\nizan\Desktop\mflix-js\node_modules\@babel\parser\lib\index.js:8187:21) {
  pos: 6807,
  loc: Position { line: 236, column: 4 },
  code: 'BABEL_PARSE_ERROR'
}

So I’m really confused at this point and have no idea what to do.

Thanks.

Hi @Nitzan_Savion, welcome to the community :wave:.
Please note that semicolons(;) are not allowed inside aggregation pipelines, please replace it with a comma(,).
Therefore the:

from: ‘comments’;
let: {‘id’, ‘$_id’};

would look like

from: ‘comments’ ,
let: {‘id’, ‘$_id’} ,

Also, to be on the safer side, please wrap the let key with a single set of double quotes.
Therefore the

let: {'id', '$_id'}

would look like

“let”: {‘id’, ‘$_id’}

In case you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

Hi @SourabhBagrecha! Thank you for your response!.
I did only use commas in the pipeline
And unfortunately, it did not work.
I also tried wrapping the let command with single quotes and double quotes and it did not work
I had tried the following:

let: {‘id’, ‘$_id’} ,
“let”: {‘id’, ‘$_id’} ,
‘let’: {‘id’, ‘$_id’} ,
$let: {‘id’, ‘$_id’} ,
“$let”: {‘id’, ‘$_id’} ,
‘$let’: {‘id’, ‘$_id’} ,

None of the above worked for me.
Is this an known issue?

They are all wrong because you are supposed to a colon where you have the first the comma. The expression { id , $_id } with whatever quotes is not valid JSON. It has to be { id : $_id }. And I recommend to always use double quotes (as in { "id" : "$_id" }) because in some languages you do not have a choice anyway. They are all string literals, so why not make it clear.

2 Likes