'Neo4j In Query with dynamic array
var arr = ["a","b","c","d"];
await session.run(`MATCH (x:Test WHERE x.name IN ${arr}`)
I use the neo4j database in nodeJs and I am writing queries but I am taking errors for arr, I tried many methods but I took arr
not defined.
Solution 1:[1]
JavaScript is not formatting the string the way you would expect:
var arr = ["a", "b", "c", "d"]
let x = `MATCH (x:Test) WHRE x.name IN ${arr}`
console.log(x)
This prints:
"MATCH (x:Test) WHRE x.name IN a,b,c,d"
Which is not valid Cypher.
To overcome this issue, just let the Neo4j driver do the formatting for you by adding a parameter to your query:
await session.run(
"MATCH (x:Test WHERE x.name IN $arr",
{arr: arr},
)
Check reference for this syntax here.
Solution 2:[2]
Try this,
var arr = ["a","b","c","d"];
await session.run(`MATCH (x:Test WHERE x.name IN ${JSON.stringify(arr)} `)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | stellasia |
Solution 2 | Parth |