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

TypeError: val[key].map is not a function at cast$expr.js:102:27 #11689

Closed
rwmia opened this issue Apr 20, 2022 · 2 comments
Closed

TypeError: val[key].map is not a function at cast$expr.js:102:27 #11689

rwmia opened this issue Apr 20, 2022 · 2 comments
Assignees
Milestone

Comments

@rwmia
Copy link

rwmia commented Apr 20, 2022

Do you want to request a feature or report a bug?
Report a Bug

What is the current behavior?
Getting an error when querying. TypeError: val[key].map is not a function at _castExpression (/opt/nodejs/node_modules/mongoose/lib/helpers/query/cast$expr.js:102:27)

If the current behavior is a bug, please provide the steps to reproduce.
When updating to 6.3 I started to get the error. I believe it may be this expression in my query below. If there is a better way to write the expression to avoid the casting error, if not a bug, please let me know.

$expr: {
          $not: {
            $in: [{
              email_id: mongoose.Types.ObjectId(email._id),
              upcoming_date: '$date_end'
            }, '$last_upcoming_notifications']
          }
 }

What is the expected behavior?
Hopefully not error :)

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node 14 (AWS Lambda)
Mongoose 6.3.0
MongoDB Atlas 4.4.13

@Uzlopak
Copy link
Collaborator

Uzlopak commented Apr 20, 2022

It seems that mongoose expects that $not is an Array. But $not is expecting an expression not an Array.

Probably it is solved by movng the $not operator from booleanComparison to comparisonOperator. Or by adding a isArray-Check after if (booleanComparison.has(key)

@rwmia
Copy link
Author

rwmia commented Apr 20, 2022

Yea the .map is causing the error, expecting an array. I have modified with:

if (booleanComparison.has(key)) {
      if (Array.isArray(val[key])) {
        val[key] = val[key].map(v => _castExpression(v, schema, strictQuery));
      }
    }

Perhaps at some point there can be a global option to not cast $expr? I.e. mongoose.set('castExpr', false);

@vkarpov15 vkarpov15 added this to the 6.3.3 milestone Apr 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants