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