gremlin-javascript 示例仅返回 Pending Promises



我一直在尝试让 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()

现在运行此脚本会向我返回某种待处理的承诺。我不需要那个。我真正需要的是节点,而不是某种形式的对象。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 }

我实际上并不真正了解这个问题。难道没有一个全面的指南可以展示一个简单的工作示例。非常感谢您帮助我了解这个问题。

gremlin-client 未连接的初始问题

当我将我的 gremlin-client 从 3.4.6 升级到 3.4.7 时,我遇到了同样的问题。我不确定发生了什么变化,但我发现在创建 DriverRemoteConnection 时指定遍历源似乎可以解决错误。

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

更新了有关获取待定承诺的问题

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

此外,查询返回的承诺将不包含整个节点。对于g.addV,完成的承诺将指示节点是否添加成功。对于g.v().has(),您需要指定所需的字段。在下面的更正代码中,我正在获取 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()

最新更新