клиент остального узла получает с локальной переменной - PullRequest
0 голосов
/ 03 ноября 2018

Я пытаюсь сделать следующее:

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

но это говорит о том, что:

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

Я хочу знать, как я могу использовать локальную переменную в запросе get? Я запрашиваю проект, который я сделал, и если я поставлю номер вместо идентификатора, он будет работать.

1 Ответ

0 голосов
/ 03 ноября 2018

1001 * попробовать *

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

Он называется литералами шаблона:

от 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.
...