Conditional upsert JSON / Javascript client

I’m writing a simple login component, where a user shall only be created if the registered email does not exist in the graph. I got the conditional upsert working, but would like to write a bit more elegantly:

    const txn = dgraphClient.newTxn();

    const query = `
      query {
          q(func: eq(registrationEmail, "${email}")) { v as uid }
      }`;
    const data = {
      "dgraph.type": "User",
      "registrationEmail": email,
    };
    const conditions = `@if( eq(len(v), 0) )`;
    const mu = new dgraph.Mutation();
    mu.setSetJson(data);
    mu.setCond(conditions);

    const req = new dgraph.Request();
    req.setQuery(query);
    req.addMutations(mu);
    req.setCommitNow(true);
    
    const res = await txn.doRequest(req);

I rewrote the last part like this:

    const res = await txn.mutate({ 
        query: query, 
        cond: conditions, 
        setJson: data, 
        commitNow: true });

However I get the error:
Database Network error TypeError: c[e].toArray is not a function

what JS client?
Cheers

I’m using dgraph-js with gRPC.

Thanks, this is important as dgraph-js-http is different.

Conditional Upserts we do like this

it is very similar to what you’re doing. Only the sets are different.

Cheers.

1 Like

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