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;
                }
              });
          }

>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?

Leave a Reply