NodeJs - Neo4j params issue on naming relationship

435 Views Asked by At

Im not able to use the params object to name a relationship between 2 nodes ,

Here is the code

var neo4j = require('neo4j');
var db = new neo4j.GraphDatabase('http://localhost:7474');

var params = {
    name: {
        firstname: "SRI",
        lastname: "lanka"
    },
    relname: "country"
};

var query = [
    'MATCH (location:PRIMARY {name:"location"})',
    'CREATE UNIQUE (location)-[:{relname}]->({name})'
].join('\n');

db.query(query, params, function(err, results) {
    if (err) throw err;
    console.log(results);

});

Here is the error.How to make use of the params to name the relationship

Error: Invalid input '{': expected whitespace or a rel type name (line 2, column 28) "CREATE UNIQUE (location)-[:{country}]->({name})"

2

There are 2 best solutions below

0
On BEST ANSWER

http://docs.neo4j.org/chunked/stable/cypher-parameters.html

Parameters can be used for literals and expressions in the WHERE clause, for the index value in the START clause, index queries, and finally for node/relationship ids. Parameters can not be used as for property names, relationship types and labels, since these patterns are part of the query structure that is compiled into a query plan.

1
On

A relationship type cannot be parameterized. The reason is that a different relationship type might lead to a different query plan.

So you should build up the cypher string using e.g. string concatenation regarding the relationship type and use Cypher parameters where appropriate.