Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

node.js - nodejs javascript promise resolve

I can't seem to figure out how to save the results of SomeQuery promise. Essentially I would like to take the value in res and pipe it into parseQuery function and return the final results. How do I make the parsed result accessible to an APIs response.

const neo4j = require('neo4j-driver')
var parser = require('parse-neo4j')
const astria_queries = require('./astriaQueries')

const uri = 'bolt://astria_graph:7687'
const user = 'xxx'
const password = 'xxx'

const someQuery = (query) => {
  //   run statement in a transaction
  const driver = neo4j.driver(uri, neo4j.auth.basic(user, password))
  const session = driver.session({ defaultAccessMode: neo4j.session.READ })
  const tx = session.beginTransaction()

  tx.run(query)
    .then((res) => {
      // Everything is OK, the transaction will be committed
      parseQuery(res)
    })
    .then(() => {
      // Everything is OK, the transaction will be committed
    })
    .catch((e) => {
      // The transaction will be rolled back, now handle the error.
      console.log(e)
    })
    .finally(() => {
      session.close()
      driver.close()
    })
}

const parseQuery = (result) => {
  try {
    const test = parser.parse(result)
    console.log(test)
  } catch (err) {
    console.log(err)
  }
}

module.exports = {
  someQuery,
}
question from:https://stackoverflow.com/questions/65931528/nodejs-javascript-promise-resolve

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It finally clicked with me. Here is the solution I came up with. Hopefully it will help others. If there is a better way please let me know. Thank you @fbiville for you help.

async actions

const neo4j = require('neo4j-driver')
var parser = require('parse-neo4j')
const astria_queries = require('./astriaQueries')

const uri = 'bolt://astria_graph:7687'
const user = 'neo4j'
const password = 'neo'

async function getRecords(query) {
  //   run statement in a transaction
  const driver = neo4j.driver(uri, neo4j.auth.basic(user, password))
  const session = driver.session({ defaultAccessMode: neo4j.session.READ })
  const tx = session.beginTransaction()
  try {
    const records = await tx.run(query)
    const parseRecords = await parseQuery(records)
    return parseRecords
  } catch (error) {
    console.log(error)
  } finally {
    session.close()
    driver.close()
  }
}

async function parseQuery(result) {
  try {
    const parsedRes = await parser.parse(result)
    // console.log(parsedRes)
    return parsedRes
  } catch (err) {
    console.log(err)
  }
}

// getRecords(astria_queries.get_data_sources)

module.exports = {
  getRecords,
}

api send()

exports.get_data_sources = async (req, res) => {
  try {
    queryFuns.getRecords(astria_queries.get_data_sources).then((response) => {
      res.send(response)
    })
  } catch (error) {
    res.status(500).send(error)
    console.log(error)
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...