typescriptneo4jcyphergoogle-cloud-functionsneo4j-driver

How to pass result value from one statement to another in Neo4j


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?


Solution

  • 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");
        })
      })
    }
    )