Error: Can't use $x with Number.
See original GitHub issueAny advice on trying to handle the validation on invalid query operator?
// The `amount` below is set as Number
Transaction
.find({ amount: { '$x': 42 } }) // note the intended invalid query operator here
.then(() => {
// ...
})
.catch((err) => {
console.log(err); // log below
if (err instanceof CastError) {
// handle if cast error
}
return next(err);
});
The log is:
Error: Can’t use $x with Number. at SchemaNumber.castForQuery (/…/node_modules/mongoose/lib/schema/number.js:278:13)
The err is not an instance of CastError so I don’t know how to specifically check the error above on mongoose. Currently, I set it as error 500 but I want it to change to something more specific error. Thanks!
Mongoose: v5.0.3 Nodejs: v8.9.3 Mongodb: v3.6.3
Issue Analytics
- State:
- Created 5 years ago
- Comments:5
Top Results From Across the Web
TypeError: can't assign to property "x" on "y": not an object
In strict mode, a TypeError is raised when attempting to create a property on primitive value such as a symbol, a string, a...
Read more >What can I do about "ImportError: Cannot import name X" or ...
I'm assume the error is due to importing entity twice - once in main.py and later in physics.py - but how can I...
Read more >"Windows cannot access the specified device, path, or file ...
Troubleshooting error message: Windows cannot access the specified device, path, or file. You may not have the appropriate permission to access the item....
Read more >If you can't send or receive messages on your iPhone or iPad
In the Settings app, tap Messages. Turn iMessage off and then back on. Tap Send & Receive. Tap the phone number that you...
Read more >Number Keys not Working in Windows 10 - 1 Simple Fix
Number keys not working in Windows 10? Unable to use numberic pad in windows 10? ... Your browser can't play this video.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@vkarpov15 should we create a specific Error type to throw for this instead of the vanilla Error that’s currently thrown? This seems like an edge case, otherwise I’d have done it already.
@ctrlnot If you want to test for this specific error condition now, you could do so in a pre query hook, and return an Error you can test in your handler. Here is an example:
6927.js
Output:
you bet 👍