MichaelE MichaelE - 4 months ago 12
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

neo4jSession
.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);

neo4jSession.close();
}) //end of if not found

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

Answer

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.