简体   繁体   中英

Neo4j JS driver cypher "WHERE" clause not working with parameters

I have a simple function that matches a User to an Id and should create a relationship like this.

const createRelation = (userId: string, todoId: string, relationship: string) => {
    return session.run(
        'MATCH (a:User),(b:Todo)' +
        'WHERE a.id = $userId AND b.id = $todoId' +
        'CREATE (b)-[r:$relationship]->(a)' +
        'RETURN r',
        {
            userId: userId,
            todoId: todoId,
            relationship: relationship,
        }
    );
}

This code does not throw any errors, but it also does not create a relationship. If I set the values for userId and todoId manually, eg to "1" , it works fine! Am I using the parameters wrong? If so, how? (I also already tried setting the relationship type manually, no relationship gets created if I use the parameters.)

Turns out you can't parametrizefor these kinds of constructs.

Parameters cannot be used for the following constructs, as these form part of the query structure that is compiled into a query plan:

  • property keys; so, MATCH (n) WHERE n.$param = 'something' is invalid
  • relationship types
  • labels

I built the string myself like so:

'WHERE a.id = ' + userId + ' AND b.id = ' + todoId +

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM