0

I am trying to do the following:

var id=9;
            client.get("http://localhost:5000/api/produto/{id}", function (data, response) {
                console.log(data);
                console.log("------------");
                console.log(response);
            });

but it says that :

{ id: [ 'The value \'{id}\'\' is not valid.' ] }

I want to know how can I use a local variable in the get request ? I am requesting to a project I've made and if I put a number instead of id it works.

shiqo
  • 103
  • 3
  • 12

1 Answers1

2

try

var id=9;
            client.get(`http://localhost:5000/api/produto/${id}`, function (data, response) {
                console.log(data);
                console.log("------------");
                console.log(response);
            });

It's called template literals:

from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

Template literals are enclosed by the back-tick (` `)  (grave accent) character instead of double or single quotes. Template literals can contain placeholders. These are indicated by the dollar sign and curly braces (${expression}). The expressions in the placeholders and the text between them get passed to a function. The default function just concatenates the parts into a single string. If there is an expression preceding the template literal (tag here), this is called a "tagged template". In that case, the tag expression (usually a function) gets called with the processed template literal, which you can then manipulate before outputting. To escape a back-tick in a template literal, put a backslash \ before the back-tick.
josesuero
  • 3,260
  • 2
  • 13
  • 19