Ashish
Ashish

Reputation: 469

Result from database with promise is not returned properly in nodeJS

I am trying to get the data from Db "neo4j"

I have created a controller and a model. Controller calls the model and the model fetches the data from the DB.

Controller is an async function which is awaiting for model function to give the data.

Model is a promise which fetches the data from DB.

Result in Model is correct, But result in controller is undefined. Please help.

    var neo4j = require("neo4j-driver").v1;

    var driver = neo4j.driver(
      "bolt://localhost:7687",
      neo4j.auth.basic("neo4j", "password")
    );
    var session = driver.session();

    function get4fomDb() {
      try {
        resultPromise = session.run("Match(n) return n");
        resultPromise.then(result => {
          session.close();
          driver.close();
          console.log("MODEL RESULT : ", result);
          return result;
        });
        resultPromise.catch(function(err) {
          console.log("===========ERROR in DB=====================");
          console.log("ERROR : ", err);
          console.log("=========================================");
          return err;
        });
      } catch (err) {
        console.log("Error : ", err);
        return "ERROR in CATCh";
        // res.send("404 : Page not found ")
      } finally {
      }
    }

    async function controller() {
      let result = await get4fomDb();
      console.log("CONTROLLER RESULT : ", result);
    }

    controller();

OUTPUT :

[nodemon] starting `node .\app.js`
CONTROLLER RESULT :  undefined
MODEL RESULT :  { records:
   [ Record {
       keys: [Array],
       length: 1,
       _fields: [Array],
       _fieldLookup: [Object] },
     Record {
       keys: [Array],
       length: 1,
       _fields: [Array],
       _fieldLookup: [Object] } ],
  summary:
   ResultSummary {
     statement: { text: 'Match(n) return n', parameters: {} },
     statementType: 'r',
     counters: StatementStatistics { _stats: [Object] },
     updateStatistics: StatementStatistics { _stats: [Object] },
     plan: false,
     profile: false,
     notifications: [],
     server:
      ServerInfo { address: 'localhost:7687', version: 'Neo4j/3.5.12' },
     resultConsumedAfter: Integer { low: 0, high: 0 },
     resultAvailableAfter: Integer { low: 0, high: 0 } } }

Upvotes: 0

Views: 310

Answers (2)

Ashish
Ashish

Reputation: 469

I Was able to close the Session Successfully in the same file :

Here is the code of the Modal:

exports.registerNew = (input) => {


    let query = "CREATE n Return (n)";


    var session = driver.session();
    return session.run(query).then(result => {
        session.close()
        driver.close()
        return result
    })
}

Upvotes: 0

ambianBeing
ambianBeing

Reputation: 3529

You aren't really returning a promise from get4fomDb (As the promise is handled there itself with then/catch).

If this session.run('Match(n) return n'); is a promise simply return it from get4fomDb and use try/catch and await in contoller(). Which makes the code more readable as well.

Also mixing patterns of async/await or promise.then().catch() is not really recommended, stick to one.

function get4fomDb() {
  return session.run("Match(n) return n");
}

async function controller() {
  try {
    let result = await get4fomDb();
    console.log("Promise results::", result);
  } catch (error) {
    console.error("Error from db promise::", error);
  } finally {
    session.close();
    driver.close();
  }
}

controller();

Upvotes: 2

Related Questions