Vialito
Vialito

Reputation: 523

How to create a node with variable label?

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.

Upvotes: 3

Views: 540

Answers (1)

stdob--
stdob--

Reputation: 29167

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}
  } 
)

Upvotes: 6

Related Questions