3

It seems I'm unable to create a node with a label based on userinput. I would like to store posted userinput from a form in a variable and pass it on to a Cypher query. While this seems to work fine for properties, it doesn't for labels. I've spend half a day on every possibility like:

('CREATE n:{typeParam} {desc:{descParam}, userID: {IDParam}}) RETURN n', {typeParam:type, descParam: desc, userID: id})

('CREATE n (SET n:{typeParam} {desc:{descParam}, userID: {IDParam}}) RETURN n', {typeParam:type, descParam: desc, userID: id})

('CREATE n:($typeParam) {desc:{descParam}, userID: {IDParam}}) RETURN n', {typeParam:type, descParam: desc, userID: id})

The first character of the the label variable is always seen as invalid input. I really wonder how to do this.

Vialito
  • 523
  • 7
  • 28
  • 1
    I dont think that's possible. Please look at the questions [here](https://stackoverflow.com/questions/21701186/setting-node-labels-with-a-parameter), [here](https://stackoverflow.com/questions/32957497/unable-to-set-node-label-dynamically-using-the-neo4j-rest-api) and [here](https://stackoverflow.com/questions/24274364/in-neo4j-how-to-set-the-label-as-a-parameter-in-a-cypher-query-from-java) – Vivek Kumar Nov 10 '17 at 07:15

1 Answers1

6

You can use the apoc.create.node procedure from the APOC library:

CALL apoc.create.node(
  // array of labels
  [{typeParam}],

  // property object
  {
    desc: {descParam}, 
    usedID: {userID}
  } 
)
stdob--
  • 28,222
  • 5
  • 58
  • 73
  • Thanks for your answer and showing the APOC library. I realized this question might be a duplicate when asking. I know I am a beginner programmer, but still, it felt weird to accept something is impossible. – Vialito Nov 13 '17 at 04:38
  • hi! but what if for each node there should be different labels? If I have an array of objects in each object I have the label and node data, Is there a way to iterate through array and create nodes with it's own labels ? – Armen Sanoyan Oct 26 '21 at 13:32
  • 1
    @ArmenSanoyan Try using UNWIND with your array of objects [https://neo4j.com/docs/cypher-manual/current/clauses/unwind/] – stdob-- Oct 26 '21 at 14:05
  • @stdob-- Hi and thx. I used unwind with apoc.merge.node. It works good but in case of nulls I get error in neo4j (java.lang.NullPointerException). Is there a way to handle nulls in neo4j ? – Armen Sanoyan Oct 27 '21 at 18:09
  • @ArmenSanoyan Hi, I think you need to create a new question with a minimal example: https://stackoverflow.com/help/minimal-reproducible-example – stdob-- Oct 28 '21 at 10:02