# "Type error" comes up when using a variable in the map function

**URL:** <https://community.influxdata.com/t/type-error-comes-up-when-using-a-variable-in-the-map-function/25825>\
**Category:** InfluxDB 2\
**Tags:** influxdb, time-series, influxdata, flux\
**Created:** [July 21, 2022, 12:49am UTC](https://community.influxdata.com/t/type-error-comes-up-when-using-a-variable-in-the-map-function/25825 "2022-07-21T00:49:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![deb](https://avatars.discourse-cdn.com/v4/letter/d/ecccb3/32.png) [@deb](https://community.influxdata.com/u/deb)\
**Post date:** [July 21, 2022, 12:49am UTC](https://community.influxdata.com/t/type-error-comes-up-when-using-a-variable-in-the-map-function/25825/1 "2022-07-21T00:49:28Z")

</div>

A query like this works fine:  
from(bucket: “X”)  
|\> range(start: 2022-01-01T00:00:00Z, stop: 2022-01-02T00:00:00Z)  
|\> group()  
|\> map(  
fn: (r) =\> ({r with Errorx: r[“Y”]})  
)

But when I replace “Y” with a variable shown below, I get an error (invalid: type error @7:37-7:38: expected int but found string)

vary = “Y”  
from(bucket: “X”)  
|\> range(start: 2022-01-01T00:00:00Z, stop: 2022-01-02T00:00:00Z)  
|\> group()  
|\> map(  
fn: (r) =\> ({r with Errorx: r[vary]})  
)

Why can’t I use a variable in the place of a column name when mapping? Please let me know how to resolve this error.

Thank you.

---

<div class="post-metadata">

**Author:** ![phill](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@phill](https://community.influxdata.com/u/phill)\
**Post date:** [July 21, 2022, 4:18pm UTC](https://community.influxdata.com/t/type-error-comes-up-when-using-a-variable-in-the-map-function/25825/2 "2022-07-21T16:18:04Z")

</div>

Hi @deb

Some time has passed without any replies, so I want to begin the dialogue. Looks like you want the value of `Errorx` to be the value in another column, but that column’s name isn’t hard-coded, it’s stored in a variable. If I’m understanding correctly, then perhaps [map’s if-then-else construct](https://docs.influxdata.com/flux/v0.x/stdlib/universe/map/) would work for you?

---

<div class="post-metadata">

**Author:** ![deb](https://avatars.discourse-cdn.com/v4/letter/d/ecccb3/32.png) [@deb](https://community.influxdata.com/u/deb)\
**Post date:** [July 21, 2022, 5:23pm UTC](https://community.influxdata.com/t/type-error-comes-up-when-using-a-variable-in-the-map-function/25825/3 "2022-07-21T17:23:21Z")

</div>

Hi @phill

Thank you for your response. Ok so with the if-then-else, all the possibilities will be included like below?

vary = “Y”  
from(bucket: “X”)  
|\> range(start: 2022-01-01T00:00:00Z, stop: 2022-01-02T00:00:00Z)  
|\> group()  
|\> map(  
fn: (r) =\> ({r with Errorx:  
if vary == “Y”  
then r[“Y”]  
else if vary == “Z”  
then r[“Z”]  
else r[“I”]  
})  
)

Why didn’t it work with r[vary]?

Thank you,

---

<div class="post-metadata">

**Author:** ![phill](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@phill](https://community.influxdata.com/u/phill)\
**Post date:** [July 21, 2022, 8:12pm UTC](https://community.influxdata.com/t/type-error-comes-up-when-using-a-variable-in-the-map-function/25825/4 "2022-07-21T20:12:35Z")

</div>

@deb In this example from the map documentation (linked above)

```auto
|> map(fn: (r) => ({time: r._time, source: r.tag, alert: if r._value > 10 then true else false}))

```

the `_value` column of the record is used as the test condition. In your case, what is determining whether to choose Y, Z or I? Let’s say it’s also the `_value` column. Then something like

```auto
|> map(fn: (r) => ({r with Errorx: if r._value == (select Y column) then r["Y"]}))
|> map(fn: (r) => ({r with Errorx: if r._value == (select Z column) then r["Z"] else r["I"]}))

```

might to the job.

A couple disclaimers: (a) I don’t think `else if` is an allowable construct in map so I’ve broken it into two maps. (b) I’ve made assumptions about your data, so this approach may not be helpful (goes back to what is determining the column choice).

I think `vary` inside map is not allowed to prevent a runtime error, that is `vary` gets set to a non-existent column (e.g., “QQ”, “21.6”).

Hope this moves you closer to a solution.  
Phill

---

<div class="post-metadata">

**Author:** ![deb](https://avatars.discourse-cdn.com/v4/letter/d/ecccb3/32.png) [@deb](https://community.influxdata.com/u/deb)\
**Post date:** [July 21, 2022, 9:41pm UTC](https://community.influxdata.com/t/type-error-comes-up-when-using-a-variable-in-the-map-function/25825/5 "2022-07-21T21:41:11Z")

</div>

Hi @phill,

Thank you for your help. I didn’t want it to depend on the \_value column, I wanted `vary` to determine the column name. I’m actually working on something different with a function, where `vary` is an input and map is used to do something else in the function. Thanks for your help, I’ll use the if-else to avoid the `r[vary]` error. The else if actually worked for me:

```auto
vary = "Y"
from(bucket: “X”)
|> range(start: 2022-01-01T00:00:00Z, stop: 2022-01-02T00:00:00Z)
|> group()
|> map(
    fn: (r) => ({r with Errorx: 
      if vary == "Y"
        then r["Y"]
      else if vary == "Z"
        then r["Z"]
      else r["I"]
    })
)

```

Thank you,

---

<div class="post-metadata">

**Author:** ![phill](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@phill](https://community.influxdata.com/u/phill)\
**Post date:** [July 21, 2022, 11:22pm UTC](https://community.influxdata.com/t/type-error-comes-up-when-using-a-variable-in-the-map-function/25825/6 "2022-07-21T23:22:09Z")

</div>

@deb Great news! I figured you gave us a code snippet of something bigger and `vary` was playing a deeper role in your script. Also, thanks for expanding my knowledge of map.
