cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Waiting on query results in javascript

I'm a rather novice javascript programmer, but I am trying to populate a drop-down select object with the results of a neo4j query. The issue I am having is that I put my neo4j query code into a function, but I can't seem to get my calling function to wait on the results. Here's the code I have right now.

function executeCypher(cypher) {
      const driver = new neo4j.driver(uri, neo4j.auth.basic(username, password));
      var parameters = new URLSearchParams(window.location.search);
      var databaseName = 'neo4j';
      if (parameters.get('database')) {
          databaseName = parameters.get('database');
      }
      const session = driver.session({database: databaseName});
      var results = [];
      console.log('Executing cypher: ' + cypher);
 
      session.run(cypher)
          .subscribe({
              onNext: (record) => {
                  results.push(record.get('n'));
              },
              onCompleted: function () {
                 console.log('Operation completed.');
                 session.close();
                 driver.close();
                 return results;
             },
             onError: function(error) {
                 console.log('Error occurred: ' + error);
                 return null;
             }
         });
}

async function populateNodesForRelationships() {
     var cypher = 'MATCH(n) RETURN n';
     var nodeList1 = document.getElementById('rel-node-1');
     var nodeList2 = document.getElementById('rel-node-2');

     var listLength = nodeList1.options.length;
     for(i = listLength-1; i >= 0; i--) {
         nodeList1.options[i] = null;
     }
     listLength = nodeList2.options.length;
     for(i = listLength-1; i >= 0; i--) {
         nodeList2.options[i] = null;
     }

     console.log('Retrieving current nodes for select lists.');
     var nodes = [];
     nodes = await executeCypher(cypher);
     **console.log('Nodes: ' + nodes);**
     if(nodes == null) {
         return
     }
     console.log('Nodes: ' + nodes);
     nodes.forEach(node => {
         console.log('Node keys: ' + node.keys);
         console.log('Node length: ' + node.length);
         var option = document.createElement("option");
         option.value = node.get('name');
         option.text = node.get('name');
         nodeList1.appendChild(option);
         nodeList2.appendChild(option);
     });
 }

The code is working in terms of executing the cypher and getting the correct response. The issue I am having is that I call the function 'executeCypher' and I need to wait for that to return before attempting to populate the list. Instead, it appears to be falling straight through. My console.log statement shows 'undefined' for the value of nodes and it prints out before any messages from the executeCypher function. So either I am using async/await wrong, or it doesn't work for Neo4j. I would assume the former. Any suggestions?

1 ACCEPTED SOLUTION

Nevermind, got it working. Just needed to wrap my session.run call inside a Promise and everything started working. Just as an FYI for anyone who might also have this issue.

View solution in original post

1 REPLY 1

Nevermind, got it working. Just needed to wrap my session.run call inside a Promise and everything started working. Just as an FYI for anyone who might also have this issue.