Gaurav Jain
Gaurav Jain

Reputation: 419

Node-Neo4j: How to check if cypherquery to delete node succeeded?

I am using node-neo4j npm module, and using the db.cypherquery() call to call cypher queries from my node js application. I am trying to delete a relationship between two nodes, and I would like to detect if the delete succeeded or failed.

Refer the code snippet below:

var cypherQuery = "MATCH (u1:User {id: '10'})-[r:LIKES]->(u2:User {id: '20'}) DELETE r;";
db.cypherQuery(cypherQuery, function(err, result){
            if(err) throw err; //does err indicate that delete failed, or something else (such as a syntax error in the cypher query)?

//do something based on whether delete succeeded or failed here

});

So, in the above, what is the best way to detect cases such as the below: 1) No matching relationship was found, so there is nothing to delete 2) Relationship was found and deleted successfully 3) Relationship was found but there was some other error in deleting it 4) There was a syntax error in the cypher script (I think this is detected via the err value)

Upvotes: 5

Views: 962

Answers (2)

rmeador
rmeador

Reputation: 25694

This is not well documented, but it is possible to access the number of nodes deleted like this:

result.summary.counters.nodesDeleted()

You'll see at my link that it references the StatementStatistics class, but that class isn't defined anywhere in the documentation (this seems like an omission, since it's a public API). You can find the definition of that class in the source of ResultSummary (and it has counters for relationships and a bunch of other stuff, too).

Upvotes: 2

Tezra
Tezra

Reputation: 8833

You can do DELETE and RETURN at the same time so DELETE r RETURN COUNT(r) will delete the matched r, and return the number of r deleted (or RETURN r for the list of r deleted)

Upvotes: 8

Related Questions