# Dgraph-nodejs client, after mutation response.getJson() is trowing error

**URL:** <https://discuss.dgraph.io/t/dgraph-nodejs-client-after-mutation-response-getjson-is-trowing-error/6272>\
**Category:** Users\
**Created:** [April 7, 2020, 2:32pm UTC](https://discuss.dgraph.io/t/dgraph-nodejs-client-after-mutation-response-getjson-is-trowing-error/6272 "2020-04-07T14:32:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![shreyansh-zazz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/shreyansh-zazz/32/3892_2.png) [@shreyansh-zazz](https://discuss.dgraph.io/u/shreyansh-zazz)\
**Post date:** [April 7, 2020, 2:32pm UTC](https://discuss.dgraph.io/t/dgraph-nodejs-client-after-mutation-response-getjson-is-trowing-error/6272/1 "2020-04-07T14:32:46Z")

</div>

I have added comments:

```javascript
const dgraph = require('dgraph-js');
const dgraphClient = require('../../config/config').dgraph.client;

module.exports = {
  adduser: async (tnxData) => {
    let res;
    const tnx = dgraphClient.newTxn();

    try {
      const mu = new dgraph.Mutation();
      mu.setSetJson(tnxData);

      // trying to get response in JSON for checking if it is success or failure
      res = (await tnx.mutate(mu)).getJson();
      
      await tnx.commit();
    } catch (e) {
      throw new Error(e);
    } finally {
      await tnx.discard();
    }

    console.log(res);
    return res;
  },
};

```

Following is the error I am getting:

```javascript
Error: SyntaxError: Unexpected end of JSON input
  

```

---

<div class="post-metadata">

**Author:** ![dpk](https://avatars.discourse-cdn.com/v4/letter/d/ea5d25/32.png) [@dpk](https://discuss.dgraph.io/u/dpk)\
**Post date:** [April 7, 2020, 2:57pm UTC](https://discuss.dgraph.io/t/dgraph-nodejs-client-after-mutation-response-getjson-is-trowing-error/6272/2 "2020-04-07T14:57:31Z")

</div>

I don’t think the double-await is required, FWIW.

You can read the response to mutate with `.toObject()`, `getJson()` only works for queries (I believe):

```auto
const response = await txn.mutate(mu);
await txn.commit();
const uid = response.toObject().uidsMap[0][n]; # where you'd have to figure out what n you want
# -or -
const uid = response.getUidsMap().get('blank-0'); # or whatever uid you need

```

---

<div class="post-metadata">

**Author:** ![shreyansh-zazz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/shreyansh-zazz/32/3892_2.png) [@shreyansh-zazz](https://discuss.dgraph.io/u/shreyansh-zazz)\
**Post date:** [April 7, 2020, 3:11pm UTC](https://discuss.dgraph.io/t/dgraph-nodejs-client-after-mutation-response-getjson-is-trowing-error/6272/3 "2020-04-07T15:11:30Z")

</div>

That double `wait` was a mistake sorry.  
I tried `.toObject()` following is the response

```javascript
{
  json: '',
  txn: {
    startTs: 217,
    commitTs: 0,
    aborted: false,
    keysList: [
      '2fdlx7dararju',
      '1e1b8h9b0v34g',
      '8dkxjyjqlsd4',
      '16y63b4b17fxf',
      'ji4ybstq7c06',
      'urgtxtgw13cf'
    ],
    predsList: [
      '1-p_mobile_number',
      '1-email',
      '1-first_name',
      '1-last_name',
      '1-middle_name'
    ]
  },
  latency: {
    parsingNs: 34341,
    processingNs: 7447967,
    encodingNs: 0,
    assignTimestampNs: 808280,
    totalNs: 8356891
  },
  metrics: undefined,
  uidsMap: [[ 'dg.711876754.35', '0x23'] ]
}

```

In Dgraph playground the ratle UI, if we make any mutation we get a response in JSON tab which contains some useful information like if it was a success or not following is an image

 ![Annotation 2020-04-07 204030](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/4/4bbf3f531af625c172a6e463968cb99dc8b93c9b.png)

I want a similar response based on which I can do better error handling.

---

<div class="post-metadata">

**Author:** ![dpk](https://avatars.discourse-cdn.com/v4/letter/d/ea5d25/32.png) [@dpk](https://discuss.dgraph.io/u/dpk)\
**Post date:** [April 7, 2020, 4:20pm UTC](https://discuss.dgraph.io/t/dgraph-nodejs-client-after-mutation-response-getjson-is-trowing-error/6272/4 "2020-04-07T16:20:42Z")

</div>

I’m not sure how to get that sort of response – don’t know enough about it. It looks like Ratel synthesizes some of those values, though:

> <https://github.com/dgraph-io/ratel/blob/b583f3da74245ba3a0f9aaaff3e6a05f7b61825f/client/src/reducers/frames.js>

---

<div class="post-metadata">

**Author:** ![paulftw](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/paulftw/32/1569_2.png) [@paulftw](https://discuss.dgraph.io/u/paulftw)\
**Post date:** [April 17, 2020, 12:50pm UTC](https://discuss.dgraph.io/t/dgraph-nodejs-client-after-mutation-response-getjson-is-trowing-error/6272/5 "2020-04-17T12:50:00Z")

</div>

Ratel uses dgraph-js-http that’s why the response structure is different.  
@shreyansh-zazz have you tried running the [dgraph-js example](https://github.com/dgraph-io/dgraph-js/tree/master/examples/simple) against your server? Did it work?

I’ll ask someone who’s maintaining dgraph-js to take a look at this issue.

---

<div class="post-metadata">

**Author:** ![shreyansh-zazz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/shreyansh-zazz/32/3892_2.png) [@shreyansh-zazz](https://discuss.dgraph.io/u/shreyansh-zazz)\
**Post date:** [April 17, 2020, 1:21pm UTC](https://discuss.dgraph.io/t/dgraph-nodejs-client-after-mutation-response-getjson-is-trowing-error/6272/6 "2020-04-17T13:21:06Z")

</div>

Hi @paulftw, I do have tried dgraph-js examples and there’s nothing much there related to getJson().

---

<div class="post-metadata">

**Author:** ![prashant-shahi](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/prashant-shahi/32/1921_2.png) [@prashant-shahi](https://discuss.dgraph.io/u/prashant-shahi)\
**Post date:** [April 19, 2020, 6:20pm UTC](https://discuss.dgraph.io/t/dgraph-nodejs-client-after-mutation-response-getjson-is-trowing-error/6272/7 "2020-04-19T18:20:07Z")

</div>

Hi @shreyansh-zazz, As mentioned by others above:

- `getJson()` is meant for queries, which can be either from `query` function or `doRequest` function. And the `mutate` function internally makes use of `doRequest` function.
- The response that you see in ratel is from dgraph-js-http client which uses Dgraph’s HTTP endpoint, while dgraph-js repository uses the gRPC endpoint. The `getJson()` function is useful for the upsert operation with queries using `doRequest` function.

Nice catch on the exception. I agree that it could be handled better.

---

<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:** [May 19, 2020, 6:20pm UTC](https://discuss.dgraph.io/t/dgraph-nodejs-client-after-mutation-response-getjson-is-trowing-error/6272/8 "2020-05-19T18:20:07Z")

</div>

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