如何在Json节点js中传递变量的值而不是变量名

时间:2021-09-10 20:37:52

Hi I'm trying to do a API call using node js and when I try to use deployerID variable in JSON instead of getting test-script it is actually printing "deployerID" and thus not letting me complete the request.

嗨,我正在尝试使用节点js进行API调用,当我尝试在JSON中使用deployerID变量而不是获取测试脚本时,它实际上是打印“deployerID”,因此不让我完成请求。

var deployerID = "test-script";
unirest.put(permURL)
    .headers({
        'Content-Type':'application/vnd.org.jfrog.artifactory.security.PermissionTarget+json',
        'X-JFrog-Art-Api' : api,
        sendImmediately: true
    }).send({
    "name": PermTargetName,
    "includesPattern": include,
    "excludesPattern": "",
    "repositories": [ "test-xyz" ],
    "principals": {
        "users" : {
            deployerID : ["r","w","n"]
        }
    }

})

Can some one tell me how to pass the value of the variable deployerID instead of "deployerID"

有人可以告诉我如何传递变量deployerID的值而不是“deployerID”

1 个解决方案

#1


3  

Surround the variable you want to use with square brackets within the JSON. This should then parse it as a variable.

使用JSON中的方括号环绕要使用的变量。然后,它应该将其解析为变量。

deployerID : ["r","w","n"]

deployerID:[“r”,“w”,“n”]

to

[deployerID]: ["r","w","n"]

[deployerID]:[“r”,“w”,“n”]


As per the comment by jfriend00 - This is ES6 syntax so requires a more up-to-date version of node.js (v5 <) and is referred to as a "computer property name".

根据jfriend00的评论 - 这是ES6语法,因此需要更新版本的node.js(v5 <),并称为“计算机属性名称”。

#1


3  

Surround the variable you want to use with square brackets within the JSON. This should then parse it as a variable.

使用JSON中的方括号环绕要使用的变量。然后,它应该将其解析为变量。

deployerID : ["r","w","n"]

deployerID:[“r”,“w”,“n”]

to

[deployerID]: ["r","w","n"]

[deployerID]:[“r”,“w”,“n”]


As per the comment by jfriend00 - This is ES6 syntax so requires a more up-to-date version of node.js (v5 <) and is referred to as a "computer property name".

根据jfriend00的评论 - 这是ES6语法,因此需要更新版本的node.js(v5 <),并称为“计算机属性名称”。