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 where not working for phone number format string "+000000000" #3621

Closed
UnclePetros opened this issue Aug 30, 2019 · 12 comments
Closed
Assignees
Labels
db:MongoDB Topics specific to MongoDB Repository Issues related to @loopback/repository package REST Issues related to @loopback/rest package and REST transport in general

Comments

@UnclePetros
Copy link

UnclePetros commented Aug 30, 2019

Steps to reproduce

  • Have a collection document to look for
  • Have a string field inside this document with a plus (+) char inside
  • Use the autogenerated find method in a controller to filter by this field

Current Behavior

No data is returned.
If I remove the plus char from the field, filter works fine.

Expected Behavior

Document data is retrieved

@raymondfeng
Copy link
Contributor

What database do you use?

@UnclePetros
Copy link
Author

mongodb

@raymondfeng
Copy link
Contributor

Is the phone number defined as string type in the model?

@UnclePetros
Copy link
Author

Yes, it is, and is required.
I'm using the like filter at moment without the +, but is not the same.

@warmansuganda
Copy link

Try request with Regex Operator

Example:
http://localhost:3000/users?filter[where][phone][regexp]=/000000000/i

Root cause:
(+) is a special character

CMIIW

@bajtos bajtos added the Repository Issues related to @loopback/repository package label Sep 2, 2019
@dhmlau
Copy link
Member

dhmlau commented Sep 27, 2019

@UnclePetros, does the above comment from @warmansuganda help resolving your issue?

@bajtos
Copy link
Member

bajtos commented Sep 30, 2019

I believe that a raw + character in URL is decoded as a space . What is the exact URL you are invoking, @UnclePetros?

What happens when you invoke the controller method from JavaScript directly, e.g. from a test?

@bajtos bajtos added the needs steps to reproduce Issues missing a small app and/or instructions for reproducing the problem label Sep 30, 2019
@UnclePetros
Copy link
Author

@UnclePetros, does the above comment from @warmansuganda help resolving your issue?

I've tried. Unfortunately, not.
No data is retrieved.

@UnclePetros
Copy link
Author

UnclePetros commented Oct 1, 2019

I believe that a raw + character in URL is decoded as a space . What is the exact URL you are invoking, @UnclePetros?

What happens when you invoke the controller method from JavaScript directly, e.g. from a test?

Ok.
I mainly use postman to do my quick tests.
I have in my document in mongodb this phone string field: "+1234567890".
I do this call from postman: localhost:3003/api/digital-users/?filter={"where":{"phone":"+1234567890"}}
No data is retrieved.

If I remove the plus char in the phone field in document in db, and remove the plus char in the api call filter, data is retrieved correctly.

Moreover, if I try to use the loopback explorer to do the query, it doesn't seems to work properly.
It retrieves always all documents in the collection no matter what filter is used.

About the special characters: I believe if I can store informations containing a special char, I should have the possibility to look for them.

Thank you.

@bajtos bajtos added the db:MongoDB Topics specific to MongoDB label Oct 1, 2019
@bajtos
Copy link
Member

bajtos commented Oct 1, 2019

Thank you for the clarification, @UnclePetros. It's important to know that you are using MongoDB, the behavior may be specific to this database.

I do this call from postman: localhost:3003/api/digital-users/?filter={"where":{"phone":"+1234567890"}}
No data is retrieved.

Here is an experiment you can try yourself:

$ node
Welcome to Node.js v12.11.0.
Type ".help" for more information.
> querystring.parse('filter={"where":{"phone":"+1234567890"}}')
[Object: null prototype] {
  'filter': '{"where":{"phone":" 1234567890"}}'
}

As you can see, Node.js is converting the plus character + to a space character , running the following query as a result:

{where: {phone: ' 1234567890'}}

You need to URL-encode values in your query, for example by calling encodeURIComponent.

$ node
> encodeURIComponent('{"where":{"phone":"+1234567890"}}')
'%7B%22where%22%3A%7B%22phone%22%3A%22%2B1234567890%22%7D%7D'

Here is a URL you can try from Postman:

localhost:3003/api/digital-users/?filter=%7B%22where%22%3A%7B%22phone%22%3A%22%2B1234567890%22%7D%7D

At minimum, you need to replace + to %22:

localhost:3003/api/digital-users/?filter={"where":{"phone":"%221234567890"}}

Moreover, if I try to use the loopback explorer to do the query, it doesn't seems to work properly.
It retrieves always all documents in the collection no matter what filter is used.

That's a known limitation of swagger-ui, the library we use in API Explorer. See #2208. It's very annoying and unfortunately also difficult to fix :(

@bajtos bajtos added question REST Issues related to @loopback/rest package and REST transport in general and removed bug needs steps to reproduce Issues missing a small app and/or instructions for reproducing the problem labels Oct 1, 2019
@bajtos
Copy link
Member

bajtos commented Oct 1, 2019

I am closing the issue as resolved. Feel free to re-open it if url-encoding your filter query does not solve the problem.

@bajtos bajtos closed this as completed Oct 1, 2019
@bajtos bajtos self-assigned this Oct 1, 2019
@pilare
Copy link

pilare commented Mar 19, 2020

I had the same problem when filtering fields containing plus characters. @bajtos thanks for the solution. However, there is a mistake in proposed replacement. Correct replacement for + is %2B. Just for anybody who will end up here later looking for help with this.

Here is my line to replace multiple plus characters:

const fixed = text.replace(/\+/g,'%2B')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
db:MongoDB Topics specific to MongoDB Repository Issues related to @loopback/repository package REST Issues related to @loopback/rest package and REST transport in general
Projects
None yet
Development

No branches or pull requests

6 participants