1

我有下面的代码,它运行多个语句,第一个语句应该返回一个由第二个语句使用的结果,它是来自android app的调用。

return session.writeTransaction(wrte=>{
    let r : any

    if(context.auth){

  //This Statement returns ID of the node

      wrte.run('MATCH (p:Person{identity:{identity}}) CREATE (p)-[po:POST]->(a:Post{post:{post},userName:{userName}}) RETURN ID(a)', 
      {identity: context.auth.uid, post: data.post, userName: context.auth.token.name })

    }
   return r as neo4j.v1.StatementResult

  })

 //how to get the ID from the Last Statement

  .then((val) =>  session.readTransaction(read => {
    console.log(val.records[0].get(0))
    return read.run('MATCH  (p:Post) WHERE id(p) = {any} RETURN p',{any: val.records[0].get(0)})
  }))

.then(result => {
    session.close();      
    driver.close()
    console.log(result)
    var singleRecord = result.records[0]
    var dat = singleRecord.get(0)

    if(result.records[0] == null){

      return  null

    } else {
      return {

        "post": dat.properties.post,
        "userName":dat.properties.userName,
    }

    }

  }).catch(error => {
    session.close()
    console.log(error)
    driver.close()
    throw new functions.https.HttpsError(error,"some error");
  })
});

console.log(val.records[0].get(0)) 返回未定义,如何正确传递结果以及如何检索 ID?

4

3 回答 3

0

我只需要return每笔交易

代码:

return session.writeTransaction(wrte=>{
    let r : any

    if(context.auth){
      r = wrte.run('MATCH (p:Person{identity:{identity}}) CREATE (p)-[po:POST]->(a:Post{post:{post},userName:{userName}}) RETURN ID(a) AS id', 
           {identity: context.auth.uid, post: data.post, userName: context.auth.token.name  })
    }

   return r as neo4j.v1.StatementResult
  }

)

  .then((r) => {
    return session.readTransaction(tx =>
      tx.run('MATCH  (p:Post) WHERE id(p) = {v} RETURN p',{v: r.records[0].get(0)})
    )

    .then(result => {
      session.close();      
      driver.close()
      var singleRecord = result.records[0]
      var dat = singleRecord.get(0)

      if(result.records[0] == null){

        return  null
        //response.send(false) 
      } else {
        return {

          "post": dat.properties.post,
          "dateTime":dat.properties.dateTime
      }
        //response.send(true) 
      }
    }).catch(error => {
      session.close()
      console.log(error)
      driver.close()
      throw new functions.https.HttpsError(error,"some error");
    })
  })
}
)
于 2019-09-23T06:30:25.030 回答
0

在您的 Cypher 声明中,您写了RETURN ID(a). 因此,您需要在 StatementResult 上访问的键称为ID(a)

使用val.records[0].get('ID(a)').

注意:如果您将此值设置为别名(例如RETURN ID(a) AS id),那么您将使用val.records[0].get('id').

于 2019-09-22T23:04:24.717 回答
0

您的第一个查询可以只返回a节点,无需第二个查询。这样效率会高很多。

而不是这个:

MATCH (p:Person{identity:{identity}})
CREATE (p)-[po:POST]->(a:Post{post:{post},userName:{userName}})
RETURN ID(a) AS id`

做这个:

MATCH (p:Person{identity:{identity}})
CREATE (p)-[po:POST]->(a:Post{post:{post},userName:{userName}})
RETURN a
于 2019-09-25T19:59:11.067 回答