Inconsistent return result of query neo4j 3.5.2


(Mail2michaelennis) #1

I am using neo4j 3.5.2 Desktop with Nodejs. I am trying to update a user record properties and add/remove relationship with other nodes in same query:
my query look like this:

                    MATCH (user:Dealer {email: $paramObj.email})
                    SET user += apoc.map.clean($paramObj, ["email","vehicles"],[])
                    WITH user, $paramObj.vehicles AS vehicles
                    UNWIND vehicles AS vehicle
                    MATCH(v:Vehicles {name:vehicle}) 
                    MERGE (user)-[r:SUPPLY_PARTS_FOR]->(v)
                    ON CREATE SET  r.since = timestamp()
                    WITH vehicles,user
                    MATCH (user)-[r:SUPPLY_PARTS_FOR]->(v)
                    WHERE NOT apoc.coll.contains(vehicles,v.name)
                    DELETE r
                    WITH $paramObj.email AS dealeremail
                    MATCH (user:Dealer {email: dealeremail})
                    RETURN user

JS Code

 commons.session
         .run(queryDB, {paramObj: paramObj})                  
         .then(dealer => {
             console.log('DEALER DATA FROM dealer records..... ' + JSON.stringify(dealer.records));

The problem is 'user' at time is empty for the same query...same Dealer. Sometimes I also get :{"code":"N/A","name":"Neo4jError"} as the value of the catch error.

At first I thought since the last query ws a delete I have no 'user' so I added a WITH and return the MATCH (user:Dealer....). That made no difference. I get 1 of 2 results ....the db is updated correctly but empty result return or code 'N/A' error.

EDIT

Ok...So I think I know what the problem is but not sure how to fix it....I was able to fix the code 'N/A' error I found a syntax error outside of the queryDB.

The issue of the returning empty 'user' array apparently is related to either not adding and/or deleting a vehicle where the result of that query would be zero rows.

How do I preserve the original 'user' result or save the email address to redo the query. I tried using WITH $paramObj.email AS dealerEmail but it seems that I cannot forward the dealerEmail...

Thought I could.


(Mail2michaelennis) #2

I believe I found an answer.... This problem is as a result of returning zero rows so it dawned on me that the OPTIONAL MATCH would also return a NULL result but with a single row with null values. So I change the MATCH searching for a relationship to delete to an OPTIONAL MATCH.

  MATCH (user:Dealer {email: $paramObj.email})
  SET user += apoc.map.clean($paramObj, ["email","vehicles"],[])
                        WITH user, $paramObj.vehicles AS vehicles
                        UNWIND vehicles AS vehicle
                        MATCH(v:Vehicles {name:vehicle}) 
                        MERGE (user)-[r:SUPPLY_PARTS_FOR]->(v)
                        ON CREATE SET  r.since = timestamp()
                        WITH vehicles,user
                        OPTIONAL MATCH (user)-[r:SUPPLY_PARTS_FOR]->(v)
                        WHERE NOT apoc.coll.contains(vehicles,v.name)
                        DELETE r
                        RETURN user    

This did the trick