2

I am trying to write a node.js app that works with neo4j, using the node-neo4j module. I am trying to submit a query from nodejs and found it wasn't working, so I tried submitting it from the neo4j browser console, in order to more easily root cause my typo, and found that it worked properly there. Any ideas as to what is going on?

The query:

Match (a:Paper { uid:26327110, citation:"Seitz A.  \"Moving beyond a binary view of specificity in perceptual learning.\" Journal of vision. 15 12 (2015 Sep 1): 1422.", date:"2015 Sep 1"}) return a
Ganesh Ghalame
  • 6,367
  • 3
  • 24
  • 29
Dude
  • 931
  • 6
  • 16

2 Answers2

2

You should probably be using parameters anyway and they might help with any encoding issues. That would look like this:

var query_string = "MATCH (a:Paper {attributes}) RETURN a"

var attributes = {uid: 26327110, citation: "Seitz A. \"Moving beyond a binary view of specificity in perceptual learning.\" Journal of vision. 15 12 (2015 Sep 1): 1422.", date: "2015 Sep 1"};

db.cypherQuery(query_string, {attributes: attributes});

Of course you can use individual parameters for individual values if you like. Not only do parameters help you not worry about encoding, but the allow Neo4j to cache the query. Also, if you are passing in values from the user parameters also help you to prevent injection attacks.

Brian Underwood
  • 10,746
  • 1
  • 22
  • 34
  • Hi Brian, that sounds really cool but I don't think db.query is a native function for the module I mentioned: [link](https://github.com/philippkueng/node-neo4j). Am I mistaken? Because that does sound pretty convenient. – Dude Sep 04 '15 at 05:29
  • I don't know if I've used `node-neo4j` before but I looked it up and it seemed to be there: http://coffeedoc.info/github/thingdom/node-neo4j/master/classes/GraphDatabase.html#query-instance – Brian Underwood Sep 04 '15 at 05:31
  • Ah, the `thingdom node-neo4j` module is a different module. I'm referring to the `philippkueng node-neo4j` module. – Dude Sep 04 '15 at 05:33
  • Ah, right, there's two of them... ;) It looks like there is a `cypherQuery` function in that one, though I'm not sure if it takes parameters. If a Cypher query function doesn't take parameters, though, I would argue that it's broken/unfinished ;) – Brian Underwood Sep 04 '15 at 05:35
  • Well, I can see both sides of it. On one side, I really like the parameter version you shared because it is nice and simple to type in terms of code, where as with the cypher query version, you are often writing in +'s to separate strings and then adding quotes and apostrophes to make sure the attributes are properly surrounded. On the other hand, you get tremendous flexibility with the cypherQuery approach. Any query you can think of, you can just put it in there. – Dude Sep 04 '15 at 05:38
  • I'm not sure I understand... You should be able to put in any query in any case. The parameters just represent the dynamic data in your query. `cypherQuery` doesn't require parameters, if that's what is confusing – Brian Underwood Sep 04 '15 at 05:41
  • I suppose I would have to see more examples of the parameter version. Are there simple ways to represent multiple unique nodes, merges / on match/on create, paths, etc? If so, then yeah I might be very much inclined to check out another module with parameter queries, just to better compare. – Dude Sep 04 '15 at 05:47
  • Parameters aren't really for merges etc... They're just to insert data into your query like `MATCH (a:Paper) WHERE a.score >= {min_score} RETURN a`. Since the query string itself doesn't change then Neo4j can also take that unique query string and cache the query plan, even if the `min_score` in this case changes. My examples was a bit out of the ordinary because it was passing in an object rather than a simple value. – Brian Underwood Sep 04 '15 at 05:50
  • You should probably check out the docs on parameters: http://neo4j.com/docs/stable/cypher-parameters.html – Brian Underwood Sep 04 '15 at 05:51
  • Well now that IS interesting... thanks for pointing this out to me! – Dude Sep 04 '15 at 05:53
  • Certainly, I love pushing parameters (in case that wasn't obvious...) – Brian Underwood Sep 04 '15 at 05:54
2

In this situation, it turns out I was just an idiot - the error occurred because I created a new database with a new password and forgot to adjust my node server's authentication. If you ever have a similar issue and see the following error in your node console: [Error: HTTP Error 401 when running the cypher query against neo4j. undefined: undefined] then you too, have a case of the idiot. As Kunal suggested in the comments to the original question, check your authentication.

Dude
  • 931
  • 6
  • 16