4

I am making a connection to the neo4j in Nodejs to get the attribute of ServiceConsumer node. But not sure, how to do it. here is my code which connect to the neo4j. Suppose ServiceConsumer has some attributes like city, state, name, userId and I have to retrieve the name of ServiceConsumer. I am getting the userId from the frontend and on the basis of this userId querying the neo4j database to get the node information.How would i get the name of ServiceConsumer? Any help ll be appreciated.

var user = this.userId;
    var request = require("request");
    var host = 'localhost';
        port = 7474;
    var httpurlforconnection ='http://' + host + ':' + port + '/db/data/transaction/commit';


    /*Let’s define a function which fires the cypher query.*/

    function runCypherQuery(query, user, callback) {
      request.post({
          uri: httpUrlForTransaction,
          json: {statements: [{statement: query, parameters: user}]}
        },
        function (err, res, body) {
          callback(err, body);
        })
    }

    // Let’s fire some queries below 

    runCypherQuery(
      'MATCH (n:ServiceConsumer {userId : {} }) RETURN n', {
        userId: 'user',
      }, function (err, resp) {
        if (err) {
          console.log(err);
        } else {
          console.log(resp);
        }
      }
    );
MicTech
  • 42,457
  • 14
  • 62
  • 79
Kunal Kumar
  • 1,722
  • 1
  • 17
  • 32

2 Answers2

1

Take a look at How to return all properties of a node with their name and their value using Cypher

You can do the same thing using nodejs, simple POST can return the whole node to you and then simply cast it to an object using JSON.

By the way, your code is working fine, you can simply take the "resp" object which should contain the result JSON.

Community
  • 1
  • 1
Supamiu
  • 8,501
  • 7
  • 42
  • 76
0

One obvious thing that I see is that you're not specifying the userId parameter. You Cypher should look something like this:

MATCH (n:ServiceConsumer {userId: {user_id}}) RETURN n

Does that help?

Brian Underwood
  • 10,746
  • 1
  • 22
  • 34
  • actually, i am passing the userId parameter at runtime, i have stored it in variable "user". userId won't be hard-coded. – Kunal Kumar Aug 12 '15 at 04:39
  • Yeah, I see that you're passing the `userId` parameter in, but you weren't specifying where it went in the query string. Just putting empty brackets like `{}` doesn't do anything, I don't think – Brian Underwood Aug 12 '15 at 12:13