CastError: Cast to Number failed for value “{ ‘$lte’: ‘36’ }” (type Object) at path “profile.age” for model “User”
Is it possible that there may be a mismatch in what mongoose is expecting for the query and what was actually passed through for the variable query? The error seems indicates "profile.age" expects a Number but an attempt to cast the object { '$lte': '36' } to a Number fails.
However, I attempted to reproduce this error but so far have not been successful. Would you be able to provide the following information:
A self-contained code example that will output this error
Hi @Mo_Ali - The behaviour described does sound odd.
On a test environment (you may need to insert the documents i’ve provided), would you be able to run the following code? I have inserted 3 documents to test it against.
do you mean set up a new project locally and try that code?
Yes, on the same environment in which you were receiving the error previously. Since the code works elsewhere (including on my machine), I was thinking that the error may be related to the project / code in which the original issue occurred. If you set up a new project and the code works, it may indicate that there may be some incorrect settings in the previous project.
Have you also tried updating to the latest version of mongoose to see if the same error occurs? I believe it is version 6.6.1 as of the time of this message.