# "wrong use of var()" error when query variable is empty

**URL:** <https://discuss.dgraph.io/t/wrong-use-of-var-error-when-query-variable-is-empty/4209>\
**Category:** Users\
**Created:** [March 18, 2019, 5:04pm UTC](https://discuss.dgraph.io/t/wrong-use-of-var-error-when-query-variable-is-empty/4209 "2019-03-18T17:04:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Awoogamuffin](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/awoogamuffin/32/1356_2.png) [@Awoogamuffin](https://discuss.dgraph.io/u/Awoogamuffin)\
**Post date:** [March 18, 2019, 5:04pm UTC](https://discuss.dgraph.io/t/wrong-use-of-var-error-when-query-variable-is-empty/4209/1 "2019-03-18T17:04:25Z")

</div>

To make this easier, I’ll begin with this mutation example. So imagine the following set up:

We have a bunch of parents which may or **may not** have a collection of items, each of which have an amount.

```auto
{
	"set": [
    {
      "uid": "_:first",
      "isParent": true,
      "name": "first",
      "items": [
      	{ "amount": 10 },
    		{ "amount": 2 }
      ]
    },
		{
      "uid": "_:second",
      "isParent": true,
      "name": "second",
      "items": [
      	{ "amount": 5 },
				{ "amount": 3 }
      ]
    },
    {
      "uid": "_:third",
      "name": "third",
      "isParent": true
    }
  ]
}

```

Notice the third node has no “items” attached to it.

Now with this query:

```auto
{
  parents(func: has(isParent)) {
    uid
    name
    items {
      itemAmount as amount
    }
    total: sum(val(itemAmount))
  }
}

```

I get the expected results:

```auto
{
  "data": {
    "parents": [
      {
        "uid": "0xc5b9",
        "name": "third"
      },
      {
        "uid": "0xc5ba",
        "name": "first",
        "items": [
          {
            "amount": 10,
            "uid": "0xc5bb"
          },
          {
            "amount": 2,
            "uid": "0xc5bc"
          }
        ],
        "total": 12
      },
      {
        "uid": "0xc5bd",
        "name": "second",
        "items": [
          {
            "amount": 5,
            "uid": "0xc5be"
          },
          {
            "amount": 3,
            "uid": "0xc5bf"
          }
        ],
        "total": 8
      }
    ]
  }

```

The problem arises if some parents are added to the database, but they do not have any “items” attached to them. So if on a fresh database we only add the following:

```auto
{
	"set": [
    {
      "uid": "_:first",
      "isParent": true,
      "name": "first"
    }
  ]
}

```

Then the above query causes this error:

```auto
"Wrong use of var() with [{itemAmount 2}]."

```

So my question is this: how can I still use that query regardless of whether or not any nodes have the “items” edge, and if not simply not display a value for “total” (as is the case for the items-less node “third” in the very first example)?

The thing is that if a single node is added with an “items” edge, then the query works fine, but I can’t guarantee there will always be a node with that edge.

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [March 18, 2019, 5:31pm UTC](https://discuss.dgraph.io/t/wrong-use-of-var-error-when-query-variable-is-empty/4209/2 "2019-03-18T17:31:47Z")

</div>

> [@Awoogamuffin](#):
>
> Then the above query causes this error:

Are you sure that you using this query?

```auto
{
  parents(func: has(isParent)) {
    uid
    name
    items {
      itemAmount as amount
    }
    total: sum(val(itemAmount))
  }
}

```

For me is working:

```auto
{
  "extensions": {
    "server_latency": {
      "parsing_ns": 30061,
      "processing_ns": 2596369,
      "encoding_ns": 881382
    },
    "txn": {
      "start_ts": 16578
    }
  },
  "data": {
    "parents": [
      {
        "uid": "0x3f7d",
        "name": "third"
      },
      {
        "uid": "0x3f7e",
        "name": "first",
        "items": [
          {
            "amount": 10
          },
          {
            "amount": 2
          }
        ],
        "total": 12
      },
      {
        "uid": "0x3f81",
        "name": "second",
        "items": [
          {
            "amount": 5
          },
          {
            "amount": 3
          }
        ],
        "total": 8
      },
      {
        "uid": "0x3f84",
        "name": "first",
        "items": [
          {
            "amount": 0
          }
        ],
        "total": 0
      },
      {
        "uid": "0x3f86",
        "name": "first"
      }
    ]
  }
}

```

I’ve also did a test with:

```auto
{
	"set": [
    {
      "uid": "_:first",
      "isParent": true,
      "name": "first",
      "items": [
      	{ "amount": 0 }
      ]
    }
  ]
}

```

Some func or indexes need at least a value. Like “0”.

Tested in:

```auto

Dgraph version : v1.0.12
Commit SHA-1 : 60d9ef0a
Commit timestamp : 2019-03-05 17:59:30 -0800
Branch : HEAD
Go version : go1.11.5

```

---

<div class="post-metadata">

**Author:** ![Awoogamuffin](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/awoogamuffin/32/1356_2.png) [@Awoogamuffin](https://discuss.dgraph.io/u/Awoogamuffin)\
**Post date:** [March 18, 2019, 5:33pm UTC](https://discuss.dgraph.io/t/wrong-use-of-var-error-when-query-variable-is-empty/4209/3 "2019-03-18T17:33:56Z")

</div>

As I said, the error only happens if, on a fresh database, I have a mutataion with only nodes that don’t have the items edge. Like so:

```auto
{
	"set": [
    {
      "uid": "_:first",
      "isParent": true,
      "name": "first"
    }
  ]
}

```

EDIT: Just to clarify, I mean first wipe the data, then only add the above mutation, from what you’ve shown you did the above set mutation on top of the existing data, so there are no errors.

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [March 18, 2019, 5:50pm UTC](https://discuss.dgraph.io/t/wrong-use-of-var-error-when-query-variable-is-empty/4209/4 "2019-03-18T17:50:07Z")

</div>

Can you share what version you are?

---

<div class="post-metadata">

**Author:** ![gus](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/gus/32/1259_2.png) [@gus](https://discuss.dgraph.io/u/gus)\
**Post date:** [March 18, 2019, 8:56pm UTC](https://discuss.dgraph.io/t/wrong-use-of-var-error-when-query-variable-is-empty/4209/5 "2019-03-18T20:56:40Z")

</div>

This is a bug. We added new logic to handle queries with null values, and that error you are seeing is obsolete now, it was from the previous logic.

Basically, Dgraph knows that the aggregate value `itemAmount` is not set (no uids match) so it will use a default value, in this case of zero, and continue on with the query.

---

<div class="post-metadata">

**Author:** ![Awoogamuffin](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/awoogamuffin/32/1356_2.png) [@Awoogamuffin](https://discuss.dgraph.io/u/Awoogamuffin)\
**Post date:** [March 18, 2019, 10:38pm UTC](https://discuss.dgraph.io/t/wrong-use-of-var-error-when-query-variable-is-empty/4209/6 "2019-03-18T22:38:00Z")

</div>

Awesome! Thanks guys- I was indeed still on .11

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/2/2b38fdece2abe5814f2e51bee69d1e67fc304b0a.png) [@system](https://discuss.dgraph.io/u/system)\
**Post date:** [April 17, 2019, 10:38pm UTC](https://discuss.dgraph.io/t/wrong-use-of-var-error-when-query-variable-is-empty/4209/7 "2019-04-17T22:38:06Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
