# Getting UndefinedFunctionError for a variable, even after it is well defined

**URL:** <https://forum.elixirforum.com/t/getting-undefinedfunctionerror-for-a-variable-even-after-it-is-well-defined/43186>\
**Category:** Questions\
**Tags:** variable, undefinedfunctionerror\
**Created:** [October 19, 2021, 10:55am UTC](https://forum.elixirforum.com/t/getting-undefinedfunctionerror-for-a-variable-even-after-it-is-well-defined/43186 "2021-10-19T10:55:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![r12543](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/r12543/32/24592_2.png) [@r12543](https://forum.elixirforum.com/u/r12543)\
**Post date:** [October 19, 2021, 10:55am UTC](https://forum.elixirforum.com/t/getting-undefinedfunctionerror-for-a-variable-even-after-it-is-well-defined/43186/1 "2021-10-19T10:55:03Z")

</div>

Hello,  
I am getting the following error. Code is also mentioned below.

`%UndefinedFunctionError{arity: 0, function: :offset, message: nil, module: nil, reason: nil}`

```elixir
{offset, _} = Integer.parse(headers["upload-offset"]). # headers["upload-offset"] returns a valid result everytime. "0", "452342" etc
upload_info = UploadCache.get(identifier)
if offset != upload_info.offset do. # here it crashes and throw the error.
        ... # doing something here
else

```

This does not happen every time. I found this issue in the production sometimes and I am not even able to reproduce this on my local machine. Can anyone give their inputs in what scenarios, this type of error can occur?

* * *

Stack trace

```elixir
msg:Ranch listener OkApi.Router.HTTP, connection process #PID<0.19205.307>, stream 3 had its request process #PID<0.18116.307> exit with reason {{%UndefinedFunctionError{arity: 0, function: :offset, message: nil, module: nil, reason: nil}, 
[{nil, :offset, 
[],
[]}, {ExTus.Actions, :patch, 3, 
  [file: 'lib/actions.ex', line: 81]}, {OkApi.TusRouter, :"-do_match/4-fun-7-", 2, 
  [file: 'lib/routers/tus_router.ex', line: 25]}, {OkApi.TusRouter, :dispatch, 2, 
  [file: 'lib/plug/router.ex', line: 284]}, {OkApi.TusRouter, :plug_builder_call, 2, 
  [file: 'lib/routers/tus_router.ex', line: 1]}, {Plug, :forward, 4, 
  [file: 'lib/plug.ex', line: 168]}, {OkApi.Router, :dispatch, 2, 
  [file: 'lib/plug/router.ex', line: 284]}, {OkApi.Router, :plug_builder_call, 2, 
  [file: 'lib/Router.ex', line: 1]}]}, {OkApi.Router, :call, 
  [%Plug.Conn{adapter: {Plug.Cowboy.Conn, :...}, assigns: %{}, before_send: 
  [], body_params: %Plug.Conn.Unfetched{aspect: :body_params}, cookies: %Plug.Conn.Unfetched{aspect: :cookies}, halted: false, host: "<some-host>", method: "PATCH", owner: #PID<0.18116.307>, params: %Plug.Conn.Unfetched{aspect: :params}, path_info: 
  ["files", "<identifier>"], path_params: %{}, port: 80, private: %{}, query_params: %Plug.Conn.Unfetched{aspect: :query_params}, query_string: "", remote_ip: {..., .., ., ...}, req_cookies: %Plug.Conn.Unfetched{aspect: :cookies}, req_headers: 
  h=[
    {"accept-encoding", "gzip"}, 
    {"authorization", "<jwt_token>"}, 
    {"connection", "keep-alive"}, 
    {"content-type", "application/offset+octet-stream"}, 
    {"host", "<some-host>.com"}, 
    {"migration_ver", "2"}, 
    {"newrelic", "...."}, 
    {"traceparent", "..."}, 
    {"tracestate", ",..."}, 
    {"tus-resumable", "1.0.0"}, 
    {"upload-metadata", "filename Y2JiMjA2OTgtZGNmYy00MTgwLWE3M2MtYzYxMGZjOTg5YWE5Lm1wNA=="}, 
    {"upload-offset", "0"}, 
    {"user-agent", "Dalvik/2.1.0 (Linux; U; Android 9; CPH2083 Build/PPR1.180610.011)"}, 
    {"user_id", "....."}, 
    {"x-forwarded-for", "....."}, 
    {"x-forwarded-port", "443"}, 
    {"x-forwarded-proto", "https"}, 
    {"x-newrelic-id", "....."}
  ], request_path: "/files/<identifier>", resp_body: nil, resp_cookies: %{}, resp_headers: 
  [{"cache-control", "max-age=0, private, must-revalidate"}], scheme: :http, script_name: 
  [], secret_key_base: nil, state: :unset, status: nil}, 
  []]}} and stacktrace 
  []

```

This is an open source function, here is the link [extus/lib/actions.ex at 273f8b15588743787ca8a9abcb9d815ee2c9f8ae · bluzky/extus · GitHub](https://github.com/bluzky/extus/blob/273f8b15588743787ca8a9abcb9d815ee2c9f8ae/lib/actions.ex#L67)

Thanks

---

<div class="post-metadata">

**Author:** ![kokolegorille](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kokolegorille/32/4784_2.png) [@kokolegorille](https://forum.elixirforum.com/u/kokolegorille)\
**Post date:** [October 19, 2021, 11:01am UTC](https://forum.elixirforum.com/t/getting-undefinedfunctionerror-for-a-variable-even-after-it-is-well-defined/43186/2 "2021-10-19T11:01:04Z")

</div>

Hello and welcome,

Can You provide the full error stack?

And the line where the error occurs 🙂

---

<div class="post-metadata">

**Author:** ![r12543](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/r12543/32/24592_2.png) [@r12543](https://forum.elixirforum.com/u/r12543)\
**Post date:** [October 19, 2021, 11:04am UTC](https://forum.elixirforum.com/t/getting-undefinedfunctionerror-for-a-variable-even-after-it-is-well-defined/43186/3 "2021-10-19T11:04:45Z")

</div>

> [@r12543](#):
>
> `if offset != upload_info.offset do.`

this line..

Stack trace

```elixir
msg:Ranch listener OkApi.Router.HTTP, connection process #PID<0.19205.307>, stream 3 had its request process #PID<0.18116.307> exit with reason {{%UndefinedFunctionError{arity: 0, function: :offset, message: nil, module: nil, reason: nil}, 
[{nil, :offset, 
[],
[]}, {ExTus.Actions, :patch, 3, 
  [file: 'lib/actions.ex', line: 81]}, {OkApi.TusRouter, :"-do_match/4-fun-7-", 2, 
  [file: 'lib/routers/tus_router.ex', line: 25]}, {OkApi.TusRouter, :dispatch, 2, 
  [file: 'lib/plug/router.ex', line: 284]}, {OkApi.TusRouter, :plug_builder_call, 2, 
  [file: 'lib/routers/tus_router.ex', line: 1]}, {Plug, :forward, 4, 
  [file: 'lib/plug.ex', line: 168]}, {OkApi.Router, :dispatch, 2, 
  [file: 'lib/plug/router.ex', line: 284]}, {OkApi.Router, :plug_builder_call, 2, 
  [file: 'lib/Router.ex', line: 1]}]}, {OkApi.Router, :call, 
  [%Plug.Conn{adapter: {Plug.Cowboy.Conn, :...}, assigns: %{}, before_send: 
  [], body_params: %Plug.Conn.Unfetched{aspect: :body_params}, cookies: %Plug.Conn.Unfetched{aspect: :cookies}, halted: false, host: "<some-host>", method: "PATCH", owner: #PID<0.18116.307>, params: %Plug.Conn.Unfetched{aspect: :params}, path_info: 
  ["files", "<identifier>"], path_params: %{}, port: 80, private: %{}, query_params: %Plug.Conn.Unfetched{aspect: :query_params}, query_string: "", remote_ip: {..., .., ., ...}, req_cookies: %Plug.Conn.Unfetched{aspect: :cookies}, req_headers: 
  h=[
    {"accept-encoding", "gzip"}, 
    {"authorization", "<jwt_token>"}, 
    {"connection", "keep-alive"}, 
    {"content-type", "application/offset+octet-stream"}, 
    {"host", "<some-host>.com"}, 
    {"migration_ver", "2"}, 
    {"newrelic", "...."}, 
    {"traceparent", "..."}, 
    {"tracestate", ",..."}, 
    {"tus-resumable", "1.0.0"}, 
    {"upload-metadata", "filename Y2JiMjA2OTgtZGNmYy00MTgwLWE3M2MtYzYxMGZjOTg5YWE5Lm1wNA=="}, 
    {"upload-offset", "0"}, 
    {"user-agent", "Dalvik/2.1.0 (Linux; U; Android 9; CPH2083 Build/PPR1.180610.011)"}, 
    {"user_id", "....."}, 
    {"x-forwarded-for", "....."}, 
    {"x-forwarded-port", "443"}, 
    {"x-forwarded-proto", "https"}, 
    {"x-newrelic-id", "....."}
  ], request_path: "/files/<identifier>", resp_body: nil, resp_cookies: %{}, resp_headers: 
  [{"cache-control", "max-age=0, private, must-revalidate"}], scheme: :http, script_name: 
  [], secret_key_base: nil, state: :unset, status: nil}, 
  []]}} and stacktrace 
  []

```

This is an open source function, here is the link [extus/lib/actions.ex at 273f8b15588743787ca8a9abcb9d815ee2c9f8ae · bluzky/extus · GitHub](https://github.com/bluzky/extus/blob/273f8b15588743787ca8a9abcb9d815ee2c9f8ae/lib/actions.ex#L67)

---

<div class="post-metadata">

**Author:** ![michallepicki](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/m/5f9b8f/32.png) [@michallepicki](https://forum.elixirforum.com/u/michallepicki)\
**Post date:** [October 19, 2021, 11:14am UTC](https://forum.elixirforum.com/t/getting-undefinedfunctionerror-for-a-variable-even-after-it-is-well-defined/43186/4 "2021-10-19T11:14:27Z")

</div>

The error is about `upload_info` being nil. `nil.offset` fails with that error

---

<div class="post-metadata">

**Author:** ![kokolegorille](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kokolegorille/32/4784_2.png) [@kokolegorille](https://forum.elixirforum.com/u/kokolegorille)\
**Post date:** [October 19, 2021, 11:15am UTC](https://forum.elixirforum.com/t/getting-undefinedfunctionerror-for-a-variable-even-after-it-is-well-defined/43186/5 "2021-10-19T11:15:14Z")

</div>

> [@r12543](#):
>
> `upload_info = UploadCache.get(identifier)`

Yes, as @michallepicki said…

You do not receive what You expect here.

---

<div class="post-metadata">

**Author:** ![r12543](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/r12543/32/24592_2.png) [@r12543](https://forum.elixirforum.com/u/r12543)\
**Post date:** [October 19, 2021, 11:19am UTC](https://forum.elixirforum.com/t/getting-undefinedfunctionerror-for-a-variable-even-after-it-is-well-defined/43186/6 "2021-10-19T11:19:47Z")

</div>

AAAHH…that might be it. Thanks a lot, I was thinking that the `offset` variable is causing the issue. Let me check that. Thanks again.
