Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter inside of map causes undefined variable item error #315

Closed
gak10100 opened this issue Mar 22, 2023 · 0 comments
Closed

Filter inside of map causes undefined variable item error #315

gak10100 opened this issue Mar 22, 2023 · 0 comments
Labels

Comments

@gak10100
Copy link

The following of a $filter inside of a $map results an Use of undefined variable: item error.

const result = aggregate(
  [
    {
      facilities: [
          {
              id: 123,
              name: 'abc corp'                    
          }
      ],
      existingFacilities: [
          {
              id: 456,
              companyName: 'abc corp'
          }
      ]
    }
  ],
  [
    {
      $project: {
          "facilities" : {
              "$map" : {
                  "input" : "$facilities",
                  "as" : "item",
                  "in" : {
                      "id" : "$$item.id",
                      "match" : {
                          "$arrayElemAt" : [
                              {
                                  "$filter" : {
                                      "input" : "$existingFacilities",
                                      "as" : "facility",
                                      "cond" : {
                                          "$and" : [
                                              "$$facility.companyName",
                                              {
                                                  "$eq" : ["$$facility.companyName", "$$item.name"]
                                              }
                                          ]
                                      }
                                  }
                              },
                              0
                          ]
                      },
                      "companyName" : "$$item.name"
                  }
              }
          }
      }
  }
  ]
);

console.log(result[0]);

The expected result:

{
    "facilities" : [
        {
            "id" : 123,
            "match" : {
                "id" : 456.0,
                "companyName" : "abc corp"
            },
            "companyName" : "abc corp"
        }
    ]
}
@kofrasa kofrasa added the bug label Mar 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants