当我使用apoc.do.when()
过程时,我在ifQuery
中得到了Neo4jError: Failed to invoke procedure 'apoc.do.when': Caused by: org.neo4j.exceptions.SyntaxException: Variable 'item' not defined (line 1, column 24 (offset: 23)) "match (pr:Product {id: item.id})"
。为什么apocalypse.do.when()
ifQuery
不能访问item
参数,而condition
可以?
`
with apoc.convert.fromJsonMap($json) as order
create (o:Order) set o = order
with o, apoc.convert.fromJsonMap($request) as request
merge (u:User {uid: o.customerId})
merge (s:Shop {uid: o.shopId})
create (u)-[rel:MADE_ORDER]->(o)<-[rel1:RECEIVED_ORDER]-(s)
with o, u, s, request
unwind o.cart as jsonItems
with o, u, s, request, apoc.convert.fromJsonMap(jsonItems) as item
CALL apoc.do.when(
item.isPromotion = false,
'match (pr:Product {id: item.id})
set pr.soldQuantity = pr.soldQuantity + 1,
pr.availableQuantity = pr.availableQuantity -1
create(u)-[:BOUGHT]->(pr)<-[:CONTAINS]-(o)
',
'match (p:Promotion {id: item.id})
set p.soldQuantity = pr.soldQuantity + 1,
pr.availableQuantity = p.availableQuantity -1
create(u)-[:BOUGHT]->(p)<-[:CONTAINS]-(o)'
,{})
YIELD value
return o as savedOrder
`
item
是从一个json字符串cart
的数组中解码出来的。为了确保我在用apocalypse.convert.fromJsonMap()
过程解码它时没有犯错误,我做了一个测试,为每个元素创建了一个Item
节点,它工作得很好。节点用它的所有参数创建。作为一个测试,我传递了要在ifQuery
和elseQuery
中使用的变量,并向它们添加了with o, u, s, request, item
,但它只是在第一个变量o
上抛出了错误。
`
with apoc.convert.fromJsonMap($json) as order
create (o:Order) set o = order
with o, apoc.convert.fromJsonMap($request) as request
merge (u:User {uid: o.customerId})
merge (s:Shop {uid: o.shopId})
create (u)-[rel:MADE_ORDER]->(o)<-[rel1:RECEIVED_ORDER]-(s)
with o, u, s, request
unwind o.cart as jsonItems
with o, u, s, request, apoc.convert.fromJsonMap(jsonItems) as item
create (i: Item) set i = item
return o as savedOrder
`
你能发现我在apocalypse.do.when()
过程中做错了什么吗?
和往常一样,非常感谢你的帮助。
1条答案
按热度按时间2admgd591#
发现问题是我没有将变量传递给过程。
所以我把它改成了
并且它如预期的那样工作。