py2neo查询返回None,但是相同的查询在neo4j控制台上有效

时间:2019-08-07 20:06:04

标签: neo4j py2neo

我有以下代码:

=ARRAYFORMULA(VLOOKUP(L2:L6, SORT(I2:J6), 2))

问题在于,即使有一个匹配条件的节点,def get_post(self, post_id): user = self.find() query = """ MATCH (user:User {id: {user_id}})-[:PUBLISHED]->(post:Post {id: {post_id}}) RETURN post """ post = graph.run(query, user_id=user['id'], post_id=post_id)[0] return post 也会返回graph.run(),正如您在 neo4j 控制台的快照中看到的那样(我替换为绑定变量以获取实际值,我验证了在调用失败时此类值正确):

enter image description here

我不知道我是否在正确使用绑定变量,或者是什么问题。

2 个答案:

答案 0 :(得分:0)

我发现问题在于graph.run()返回一个光标,因此您必须遍历它才能访问其内容。所以这是实际的功能代码:

def get_post(self, post_id):
    user = self.find()
    query = """
    MATCH   (user:User {id:{user_id}})-[r:PUBLISHED]->(post:Post {id:{post_id}})
    RETURN  post
    """
    post = graph.run(query, user_id=user['id'], post_id=post_id)
    if post.forward():
        return post.current['post']
    else:
        return None

答案 1 :(得分:0)

您也可以使用graph.data()代替graph.run()

def get_post(self, post_id):
    user = self.find()
    query = """
    MATCH   (user:User {id:{user_id}})-[r:PUBLISHED]->(post:Post {id:{post_id}})
    RETURN post AS result
    """
    posts = graph.data(query, user_id=user['id'], post_id=post_id)
    for node in posts:
        print node["result"]