-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
findOne() returns non-empty result if a value of filtering criteria is undefined #637
Comments
This looks like a bug to me. @raymondfeng could you please confirm? @voitau Would you mind to submit a pull request? The implementation of |
As you have noticed, we remove For case 2 |
@raymondfeng, DB is a memory db, Car.number property is of string type: {
"name": "Car",
"plural": "Cars",
"base": "PersistedModel",
"properties": {
"number": {
"type": "string",
"required": true
}
}
} I shared the complete sample project here: https://github.com/voitau/loopback-example-findbyundefined
So, here is my understanding of difference:
I can certainly submit a pull request for this method. I also think it may be related to find() method, which I can check as well and the behavior probably has to be consistent. The only ramification of this change I see is that some existing applications using this functionality can be broken due to the reason I'm facing myself during development: let's assume there is a service method, which accepts the query. The query for some reason was not populated on a higher level. As a result while the query is incorrect, there is still a record in the response. @bajtos, @raymondfeng, please let me know if this does make sense, so that I can start working on a fix for this. |
I don't have a strong opinion here. Since I am slightly inclined to keep it as it is for the sake of backwards compatibility. Perhaps add a warning to notify the developer that the query may not be expressing what was their intent. @voitau How did you run into this issue? What kind of a high-level problem are we trying to solve here? |
I would prefer the following:
The current implementation probably treats 4 & 5 the same as 3. |
@bajtos The high-level problem is the implementation of the method, which does some custom logic based on presence of a certain model instance, where presence is defined by a search by specified fields from the query model instance, passed as an argument in the body. What is being done:
SomeModel.confirm = function(someModelQuery, next) {...} and remote method signature including: accepts: { arg: 'data', type: 'SomeModel', http: { source: 'body' } } where
SomeModel.findOne({ where: {field1: someModelQuery.field1, field2: someModelQuery.field2} }, cb) If REST API client will pass the empty query to |
@raymondfeng here is how mongodb shell behaves in all these cases (maybe somehow this will be useful): > db.cars.find({})
{ "_id" : ObjectId("5444349871af283b92c440cc"), "number" : "1234" }
{ "_id" : ObjectId("54447d9a71af283b92c440cd"), "number" : null }
{ "_id" : ObjectId("54447e7571af283b92c440ce"), "number" : "" }
> db.cars.findOne({})
{ "_id" : ObjectId("5444349871af283b92c440cc"), "number" : "1234" }
> db.cars.findOne({"number":"1234"})
{ "_id" : ObjectId("5444349871af283b92c440cc"), "number" : "1234" }
> db.cars.findOne({"number":null})
{ "_id" : ObjectId("54447d9a71af283b92c440cd"), "number" : null }
> db.cars.findOne({"number":""})
{ "_id" : ObjectId("54447e7571af283b92c440ce"), "number" : "" }
> db.cars.findOne({"number":undefined})
2014-10-19T20:25:02.851-0700 error: {
"$err" : "Can't canonicalize query: BadValue cannot compare to undefined",
"code" : 17287
} at src/mongo/shell/query.js:131 |
IMO undefined should not be allowed as a search parameter value. It has no meaning with respect to the column values in the data base. Therefore the API must implement a rule to handle undefined search values. It would be simpler to disallow undefined search values. |
👍 Silently stripping out undefined values can have catastrophic outcomes for MongoDB queries.
The result of the above code is that all users in the database will have their email updated. Although this also requires an application error, this is not an expected outcome for the developer in any circumstance. |
To add to this a worse outcome which we have experienced was a rouge client bug removed all documents from our collection: cc @raymondfeng |
I would prefer the ability to set a configuration to:
|
@jrschumacher +1. Do you want to submit a patch? |
@raymondfeng if you can point me to which method is stripping the undefined values I will have a patch you you by Wednesday! Spent 4 hours tracing through code and not a hair close. |
@jrschumacher it's in datasource-juggler Keep in mind that it is used internally as well, and sometimes should strip undefined values. |
@kblcuk thanks! I'll keep that in mind. |
@jrschumacher great idea |
@raymondfeng can you look at loopbackio/loopback-datasource-juggler#725 and give me some feedback. |
@voitau @esco a patch to this has been released in [email protected] you can use it by adding
@bajtos this issue can probably be closed now? |
Excellent:exclamation: :+1: thanks guys |
Thank you! |
This comment has been minimized.
This comment has been minimized.
can we make findOne should return null if no condition is matched? cuz now it returnz empty object to me |
@bilal68 please open a new issue to discuss the problem you are experiencing, and include a small application we can use to reproduce it - see https://loopback.io/doc/en/contrib/Reporting-issues.html#loopback-4x-bugs |
Basic cases:
Question:
Could you please clarify whether this is an expected behavior.
Some more details:
The text was updated successfully, but these errors were encountered: