Search code examples
node.jsneo4jkoa

Can't pass paremeters using KOA-NEO4J library


I am trying to create a REST API which connects to a NEO4J instance. I am using the koa-neo4j library as the basis (https://github.com/assister-ai/koa-neo4j-starter-kit).

This first example below works fine. When I hit the endpoint "myapp/metric" I get the expected result back from the database.

Index.js

//Return all metrics from the database
app.defineAPI({
    method: 'GET',
    route: '/api/v1/imm/metric',
    cypherQueryFile: './src/api/v1/imm/metric/metric.cyp'
});

metric.cyp

MATCH (a:metric)
RETURN a AS metric

I am trying to build on this example creating an endpoint where I can pass a parameter that will be used in the cypher query I have followed the documentation and ended up with the below.

Index.js

//Return all metrics from the database matching a specific metric name
app.defineAPI({
    method: 'GET',
    route: '/api/v1/imm/metric/:metricname',
    cypherQueryFile: './src/api/v1/imm/metric/metric-by-name.cyp'
});

metric-by-name.cyp

MATCH (a:metric {name: $metricname })
RETURN a AS metric

When I hit the endpoint "myapp/metric/TestMetricName" I just get an error message back which is coming from Neo4j.

ConflictError: error while executing Cypher: Error: Variable $metricname not defined (line 1, column 24 (offset: 23)) "MATCH (a:metric {name: $metricname })"

It looks like the code is not swapping out the placeholder $metricname in the cypher query for the value that I have passed (e.g. TestMetricName).

Any help appreciated.


Solution

  • Since 3.1.10-M06 new syntax for parameters in Cypher ($param instead of {param}). The documentation and example for the library koa-Neo4j uses the latest version.

    So try {param} instead $param.

    https://github.com/neo4j/neo4j/wiki/Neo4j-3.1-changelog#310-m06

    https://github.com/neo4j/neo4j/pull/7558