无法将变量值解析为数组变量 [英] Unable to Parse the variable value to the array variable

查看:56
本文介绍了无法将变量值解析为数组变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用下面的空手道代码将变量'i'的值传递给数组索引'locations [i]'.但是抛出一个错误,说无法解析.请提出任何更改建议.

I was trying to pass the variable 'i' value to a array index 'locations[i]' using below karate code. but throwing an error saying unable to parse. Please suggest be for any changes.

Feature: Verify Branches

Background: For loop implementation
    Given url ''
    When method GET
    Then status 200  
    * def i = 0
    * def z = $.locations[i].zip
    * def p = $.locations[i].phone
    * def fun = 
    """ 
    function(locations){ 
        for (var i = 0; i < locations.length; i++)
        {
            print(i)
            print('Element at Location ' + i +':' + p)
        }
    }
    """ 

Scenario: Validate the locations
    Given url ''
    When method GET
    Then status 200  
    * call fun p

推荐答案

由于您未提供response的值,因此很难区分任何内容.这里有很多错误.但是我会尽力的.

It is hard to make out anything since you have not provided the value of the response. There are many things wrong here. But I'll try.

上一行:

* def z = $.locations[i].zip

这将不起作用,默认情况下,空手道不支持JsonPath中的变量,请参考文档: https://github.com/intuit/karate#jsonpath-filters

This will not work, Karate does not support variables within JsonPath by default, refer the docs: https://github.com/intuit/karate#jsonpath-filters

我认为您不必要使用JsonPath,而使用普通的JavaScript就足够了:

And I think you are un-necessarily using JsonPath where normal JavaScript would have been sufficient:

* def z = response.locations[i].zip

另外,看来您只是试图遍历数组并调用功能.请参阅数据驱动功能上的文档.

Also it seems you are just trying to loop over an array and call a feature. Please refer to the documentation on Data Driven Features.

请花一些时间阅读文档和示例,这将是您的宝贵时间.另一个提示-在我离开您以更好地了解空手道之前.有一种方法可以在需要时将JSON数组转换为另一个JSON数组:

Take some time and read the docs and examples please, it will be worth your time. One more tip - before I leave you to understand Karate a little better. There is a way to convert a JSON array into another JSON array should you need it:

* def fun = function(x){ return { value: x } }
* def list = [1, 2, 3]
* def res = karate.map(list, fun)
* match res == [{ value: 1 }, { value: 2 }, { value: 3 }]

因此,您完全不需要手动进行for循环.

So there should never be a need for you to manually do a for loop at all.

这篇关于无法将变量值解析为数组变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆