JavaScript -无法设置未定义的属性- Apigee

2izufjch  于 2023-05-16  发布在  Java
关注(0)|答案(1)|浏览(104)

我一直得到下面的错误

{
    "fault": {
        "faultstring": "Execution of Testing1 failed with error: Javascript runtime error: \"TypeError: Cannot set property \"case\" of undefined to \"120\". (Testing1.js:3)\"",
        "detail": {
            "errorcode": "steps.javascript.ScriptExecutionFailed"
        }
    }
}

我的代码如下

var Backend_Response = JSON.parse(context.getVariable('response.content'));
Backend_Response.test.case = 120;
context.setVariable('response.content',JSON.stringify(Backend_Response));

我尝试在我的代码中添加一个嵌套的JSON,并将case作为嵌套对象。预期输出为

{
    "Primary": "Status",
    "test": {
        "case": 120
    }
}

谁能帮我解决这个问题。

ovfsdjhp

ovfsdjhp1#

谢谢Barmer的即时回复。我在下面试过了,现在起作用了。

var Backend_Response = JSON.parse(context.getVariable('response.content'));
Backend_Response.test = {};
Backend_Response.test.case = 120;
context.setVariable('response.content',JSON.stringify(Backend_Response));

相关问题