Neo4j错误:调用过程'apoc.do.when `失败:变量未定义,但已定义

4zcjmb1e  于 2022-11-05  发布在  其他
关注(0)|答案(1)|浏览(217)

当我使用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节点,它工作得很好。节点用它的所有参数创建。作为一个测试,我传递了要在ifQueryelseQuery中使用的变量,并向它们添加了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()过程中做错了什么吗?
和往常一样,非常感谢你的帮助。

2admgd59

2admgd591#

发现问题是我没有将变量传递给过程。

,{})

所以我把它改成了

,{
  item: item,
  o: o,
  u: u,
  s: s
      })

并且它如预期的那样工作。

相关问题