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
294 views
in Technique[技术] by (71.8m points)

node.js - I can't find a way to get data from gremlin-server, using node/express - promise {<pending>} forever

Before you read on too far, this question might be about Node/Express or it might be about Gremlin - I can't work out which is the cause?

On my Linux box as SU, I've started Janusgraph: docker run --name janusgraph-default --volume /home/greg/Downloads/gremlin:/dataImports janusgraph/janusgraph:latest (I mount a local folder, to pick up the air-routes.graphml file)

On the same box, in a new terminal I start gremlin-server: docker run --rm --link janusgraph-default:janusgraph -e GREMLIN_REMOTE_HOSTS=janusgraph -it janusgraph/janusgraph:latest ./bin/gremlin.sh (which gives me a gremlin console)

From the gremlin console, I can run :remote connect tinkerpop.server conf/remote.yaml

Then :> graph.io(graphml()).readGraph("/dataImports/air-routes.graphml")

Finally: :> g.V().has('airport','code','DFW').values(), which returns the details for Dallas Fort Worth, as expected.

Having demonstrated that I've loaded the data successfully and can query it from the console, I want to create a Node/Express program that will ultimately run some generally static queries to build a series of dashboard pages.

I have cobbled together my first Express app:

const express = require('express');
const app = express();
const router = express.Router();

const gremlin = require('gremlin');
const traversal = gremlin.process.AnonymousTraversalSource.traversal;
const __ = gremlin.process.statics;
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
const column = gremlin.process.column;
const direction = gremlin.process.direction;
const p = gremlin.process.P;
const textp = gremlin.process.TextP;
const pick = gremlin.process.pick;
const pop = gremlin.process.pop;
const order = gremlin.process.order;
const scope = gremlin.process.scope;
const t = gremlin.process.t;
// const tq = traversal().withRemote(new DriverRemoteConnection('ws://localhost:8182/gremlin'));
const tq = traversal().withRemote(new DriverRemoteConnection('ws://localhost:8182'));
const g = tq;

router.get('/home', (req,res) => {
  res.send('<h1>Hello World,</h1><p>This is home router</p>');
});

// my first pass
// router.get('/query', async (req,res) => {
//     const response = await gremlinQuery();
//     //const response = gremlinQuery();
//     const resp = res.send(response);
// });

//my second pass
router.get('/query', (req,res) => {
  const response = gremlinQuery();
  console.log(response);
  const resp = res.send(response);
});

//<snipped out other router.gets>

app.use('/', router);

app.listen(process.env.port || 3000);

console.log('Web Server is listening at port '+ (process.env.port || 3000));

async function gremlinQuery() {
  let results;
//function gremlinQuery() {
    try{
        const results = await g.V().has('airport', 'code', 'LHR').values().toList();
        console.log(results);
        //JsonConvert.DeserializeObject<results>(JsonConvert.SerializeObject(results2));
        var arr = JSON.parse(results);
        console.log('output: ' + arr);
        return('output: ' + arr);
    } catch (error) {
            console.error(error);
    }
}

The query in the code returns the expected result in gremlin-console but in this code (which I run in debug mode in VSCode) it launches the expected webpage on localhost:3000 but without the expected JSON response data and on the debug terminal it stops at Promise {<pending>} and I can't work out whether I'm a) not connecting to the gremlin-server? b) have made a mess of my async/await (is it OK that it's on the function called by the router function?) c) Something else

(got to say the 'net isn't awash with Node/Express/Gremlin/Janusgraph examples to draw on.)


Docker version 19.03.11, build 42e35e61f352

Linux xxxxxxxx 5.3.18-lp152.60-default #1 SMP Tue Jan 12 23:10:31 UTC 2021 (9898712) x86_64 x86_64 x86_64 GNU/Linux

gremlin> Gremlin.version() ==>3.4.6

node --version v14.15.4

express ??

question from:https://stackoverflow.com/questions/66054964/i-cant-find-a-way-to-get-data-from-gremlin-server-using-node-express-promise

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

1 Answer

0 votes
by (71.8m points)

gremlinQuery is a async function, then you have wait until the function resolve a result.

And gremlinQuery look like an unused function (???)

How about

async function gremlinQuery() {
  const results = await g.V().has('airport', 'code', 'LHR').values().toList();
  // return JSON.parse(results); // results is a string???
  return results;
}

express router

router.get('/query', async (req, res) => { // async handle
  try {
    const response = await gremlinQuery(); // wait until the query response
    // res.send(response);
    res.json(response); // send it to client as a json object
  } catch (error) {
    res.status(500).send(error); // Notice to client - Some thing went wrong
  }
});

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

...