gremlin-javascript示例只返回Pending Promises。

问题描述 投票:1回答:1

我一直在尝试让gremlin与nodejs一起工作。我发现我可以使用其中一个gremlin驱动程序 gremlin-javascript 来实现这一目标。一个快速可行的脚本如下。

const gremlin = require('gremlin');
const Graph = gremlin.structure.Graph;
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
const graph = new Graph();

const g = graph.traversal().withRemote(new DriverRemoteConnection('ws://localhost:8182/gremlin', { traversalSource: 'g' }));

const addUser1 = async () => {
    const newVertex = await g.addV("id", 1, "label", "person", "name", "marko", "age", 29)
    console.log("Vertex added: ", newVertex)
}

const traverse = async () => {
    const traverse = await g.V().has("name", "marko")
    console.log(traverse)
}

addUser1()
traverse()

现在运行这个脚本会给我返回一个Pending Promise(待定的承诺)。而我不需要这个。我真正需要的是节点,而不是某种形式的对象。结果为 addUser1() 给出如下。

Promise {
  <pending>,
  domain:
   Domain {
     domain: null,
     _events: { error: [Function: debugDomainError] },
     _eventsCount: 1,
     _maxListeners: undefined,
     members: [ [Object] ] } }
> Vertex added:  GraphTraversal {
  graph: Graph {},
  traversalStrategies: TraversalStrategies { strategies: [ [Object] ] },
  bytecode: Bytecode { sourceInstructions: [], stepInstructions: [ [Array] ] },
  traversers: null,
  sideEffects: null,
  _traversalStrategiesPromise: null,
  _traversersIteratorIndex: 0 }

而对于 纵贯():

Promise {
  <pending>,
  domain:
   Domain {
     domain: null,
     _events: { error: [Function: debugDomainError] },
     _eventsCount: 1,
     _maxListeners: undefined,
     members: [ [Object] ] } }
> GraphTraversal {
  graph: Graph {},
  traversalStrategies: TraversalStrategies { strategies: [ [Object] ] },
  bytecode:
   Bytecode {
     sourceInstructions: [],
     stepInstructions: [ [Array], [Array] ] },
  traversers: null,
  sideEffects: null,
  _traversalStrategiesPromise: null,
  _traversersIteratorIndex: 0 }

其实我真的不明白这个问题。是不是有一个全面的指南,可以显示一个简单的工作例子。如果你能帮助我理解这个问题,我将非常感激。

javascript node.js gremlin
1个回答
3
投票

gremlin-client无法连接的初始问题。

当我把我的gremlin-client从3.4.6升级到3.4.7时,我也遇到了同样的问题。我不确定是什么改变了,但我发现在创建DriverRemoteConnection时指定traversalSource似乎可以解决这个错误。

const g = traversal().withRemote(new DriverRemoteConnection('ws://localhost:8182/gremlin', { traversalSource: 'g' }));

更新了关于获取未决承诺的问题

你的addUser1查询缺少一个终端步骤。你可能习惯于使用自动添加终端步骤的gremlin控制台,但对于javascript客户端,你需要手动终止终端步骤。但是对于javascript客户端,你需要手动终止查询。Iterator 带着 .next() Stephen Mallette在这个答案中很好地解释了这个问题。https:/stackoverflow.coma340193263834802。

此外,查询返回的承诺不会包含整个节点。对于 g.addV承诺完成后,将指示节点是否被成功添加。对于 g.v().has()你需要指定你需要的字段。在下面的修正代码中,我正在获取marko的年龄。

最后,在下面的代码中,我获取了marko的年龄。g.addV(key1, value1, key2, value2, ...) 我以前没有遇到过或测试过这种格式。g.addvV(label).property(key1,value1).property(key2,value2) 是我熟悉的格式,所以我在下面的答案中加入了这个。

const gremlin = require('gremlin');
const Graph = gremlin.structure.Graph;
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
const graph = new Graph();

const g = graph.traversal().withRemote(new DriverRemoteConnection('ws://localhost:8182/gremlin', { traversalSource: 'g' }));

const addUser1 = async () => {
    try {
        const newVertex = await g.addV('person').property('id',1).property('name','marko').property('age',29).next()
        //curious what this console log prints 
        console.log("Vertex added: ", newVertex)
    } catch (e) {
        console.error(e);
    }

}

const traverse = async () => {
    try {
        const traverse = await g.V().has("name", "marko").values('age').toList()
        console.log(traverse)
    } catch (e) {
        console.error(e)
    }
}

addUser1()
traverse()
© www.soinside.com 2019 - 2024. All rights reserved.