Gremlin 查询 Node.js 中的循环 JSON 问题

Circular JSON issue in Node.js on Gremlin query

JanusGraph 运行正在 localhost:8182 上 Docker 容器中

我 运行ning gremlin console 在链接到 Janusgraph Docker 容器的 Docker 容器中

我可以运行成功遍历导入的air-routes.graphml数据

我正在构建一个 Node.js/Express 应用程序,如下所示:

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 g = traversal().withRemote(new DriverRemoteConnection('ws://localhost:8182/gremlin'));

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

router.get('/query', (req,res) => {
    // g.V().has('airport','code','DFW').values().toList()
    // .then(DFWdetail => console.log(DFWdetail));
    res.send(gremlinQuery());
    //res.send("<h1>Airport query</h1><p>g.V().has('airport','code','DFW').values()</p>");
  });

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

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

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

app.use('/', router);

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

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

function  gremlinQuery() {
    const info = g.V().has('airport', 'code', 'DFW').values();
    console.log(info);
    return(info);
}

当我 运行 这个节点代码时,我得到一个命令行响应:


Web Server is listening at port 3000
GraphTraversal {
  graph: Graph {},
  traversalStrategies: TraversalStrategies { strategies: [ [RemoteStrategy] ] },
  bytecode: Bytecode {
    sourceInstructions: [],
    stepInstructions: [ [Array], [Array], [Array] ]
  },
  traversers: null,
  sideEffects: null,
  _traversalStrategiesPromise: null,
  _traversersIteratorIndex: 0
}
TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'GraphSON3Reader'
    |     property '_deserializers' -> object with constructor 'Object'
    |     property 'g:Traverser' -> object with constructor 'TraverserSerializer'
    --- property 'reader' closes the circle
    at JSON.stringify (<anonymous>)
    at stringify (/home/greg/Development/airlines/node_modules/express/lib/response.js:1123:12)
    at ServerResponse.json (/home/greg/Development/airlines/node_modules/express/lib/response.js:260:14)
    at ServerResponse.send (/home/greg/Development/airlines/node_modules/express/lib/response.js:158:21)
    at /home/greg/Development/airlines/myapp.js:28:9
    at Layer.handle [as handle_request] (/home/greg/Development/airlines/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/greg/Development/airlines/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/greg/Development/airlines/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/greg/Development/airlines/node_modules/express/lib/router/layer.js:95:5)
    at /home/greg/Development/airlines/node_modules/express/lib/router/index.js:281:22

我用 Google 搜索了 CircularJSON 并相信它可能与 await 有关,但是当我尝试它时

function  gremlinQuery() {
    const info = await g.V().has('airport', 'code', 'DFW').values();
    console.log(info);
    return(info)

VSCode 声称它是多余的并将其删除

我很困惑。我可以做些什么来构建一个基本的 Node.js/Express 遍历,returns 来自航线图数据?

我认为你需要 "iterate your traversal" 因此:

function  gremlinQuery() {
    const info = await g.V().has('airport', 'code', 'DFW').values().toList();
    console.log(info);
    return(info)

基本上,像 g.V().has('airport', 'code', 'DFW').values() returns 这样的一行 Traversal 对象本身什么都不做,因为它是 Iterator (or perhaps more explicitly has the behavior of a Java Iterator). You have to supply some kind of terminating step 的一种形式来获得结果(在这种情况下我用了 toList()).