MichaelE MichaelE - 1 year ago 55
Node.js Question

What's the correct syntax to check if cypher query return zero row in javascript

I am trying to determine if a username exists before creating the user. I am using the following code. I need an elegant way to determine if the zero row is returned ...username doesn't exist. for example I know the returned row value would be zero if not found. How can I get access to the row value in the code. Can someone assist...thanks...BTW I am using neo4j3.0 Nodejs with express and Passport

.run(MATCH (user {email: newUser.email}) RETURN user);
.then (function(result) {
if ((not found) {
.run(CREATE (user: {email:newUser.email, password:newUser.password} ) ASSERT email is UNIQUE RETURN user);

}) //end of if not found

else (found)
// email address already exist
console.log("email address already exist");
}); //end .then
.catch(function(error) {

Answer Source

Query (use counter):

MATCH (user {email: newUser.email}) 
RETURN count(user)=1 as user_exists

In javascript:

if ( result.records[0].get('user_exists') !== true ) {
  // create new user

And, of course, add a unique constraint to the email address for the user.