1

I have the below code where it runs multiple statements, First Statement should return a result which is used by Second Statement, it is a call from 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)) returns undefined, how to properly pass results and how to retreive ID?

VINNUSAURUS
  • 1,518
  • 3
  • 18
  • 38

3 Answers3

0

In your Cypher statement, you wrote RETURN ID(a). Therefore, the key that you need to access on the StatementResult is called ID(a).

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

Note: If you alias this value (e.g. RETURN ID(a) AS id), then you will do val.records[0].get('id').

Joey Kilpatrick
  • 1,394
  • 8
  • 20
0

I just had to return every transaction

Code:

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");
    })
  })
}
)
VINNUSAURUS
  • 1,518
  • 3
  • 18
  • 38
0

Your first query could just return the a node, eliminating the need for a second query. That would be a lot more efficient.

Instead of this:

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

do this:

MATCH (p:Person{identity:{identity}})
CREATE (p)-[po:POST]->(a:Post{post:{post},userName:{userName}})
RETURN a
cybersam
  • 63,203
  • 6
  • 53
  • 76
  • You are right when we use it in normal queires, but this GraphAware UUID Library (https://github.com/graphaware/neo4j-uuid/blob/master/README.md) requires first to create and query for UUID in another statement. – VINNUSAURUS Sep 26 '19 at 02:11