【问题标题】:node rest client get with local variable节点休息客户端获取本地变量
【发布时间】:2019-04-07 12:53:31
【问题描述】:

我正在尝试执行以下操作:

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.' ] }

我想知道如何在获取请求中使用局部变量? 我正在请求我所做的一个项目,如果我输入一个数字而不是 id,它就可以工作。

【问题讨论】:

    标签: javascript node.js rest api node-rest-client


    【解决方案1】:

    试试

    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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-11
      相关资源
      最近更新 更多