Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to set the dynamic search parameter correctly in the url?

**Am I setting the dynamic search parameter correctly in the url? Or I should use the backticks or other syntacsis? I need to search by the input the objects inside a backend url endpoint.

https://codesandbox.io/s/onscroll-izfjoc?file=/index.html**

people: [],
getAllProducts(searchInput) {
            let searchUrl = searchInput;
            fetch("http://test.mosvesna.com/?search=${searchUrl}")
              .then((response) => response.json())
              .then((res) => {
                let local = this;
                if (searchInput) {
                  this.people = res.filter((i) =>
                    i.name.toLowerCase().includes(searchInput.toLowerCase())
                  );
                } else {
                  this.people = res;
                }
              });
          }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Yes, you need to use backticks to be able to use string interpolation

But the code you posted doesn’t make much sense in a real world scenario.
If you’re sending a search parameter to the API then one would expect you’re going to get filtered data.
That means you don’t have to filter it yourself.

Also, you’re assigning a lot of variables which you don’t use later on. What’s the purpose of searchUrl and local variables?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading