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

I have added comments:

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:

Error: SyntaxError: Unexpected end of JSON input
  

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):

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

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

{
  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

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

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:

1 Like

Ratel uses dgraph-js-http that’s why the response structure is different.
@shreyansh-zazz have you tried running the dgraph-js example against your server? Did it work?

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

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

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.

1 Like

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