在 gremlin-node 中设置 属性。js/gremlin-server 不工作

Set property in gremlin-node.js/gremlin-server not working

我想使用 gremlin 的 javascript 驱动程序从 node.js 后端在 gremlin-server 中创建一个图表。当我添加两个属性,一个 id 和一个用户名时,id 有效,用户名未存储。这是代码:

const gremlin = require('gremlin');

const traversal = gremlin.process.AnonymousTraversalSource.traversal;
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;

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

const { t: { id } } = gremlin.process;
const { cardinality: { single} } = gremlin.process;

    async function createUser(userid,username) {
        const vertex = await
            g.addV('User')
            .property(id,userid)
            .property(single,'username',username)
            .iterate();
        return vertex;
    }
await createUser(1001,"testuser")

输出是(当我用 g.V(1001).listAll(); 搜索节点时)属性总是 undefined.

[Vertex { id: 1001, label: 'User', properties: undefined }]

gremlin 服务器是 loaded/run,docker 使用以下命令:

我试过使用和不使用上面的单基数,添加了更多属性,但都没有用。互联网搜索显示了一些带有 .property 步骤的 gremlin-console(groovy) 工作示例,但没有提示组合 node.js-gremlin 驱动程序和 gremlin-server.

我想您的代码工作正常并且属性存在。问题是从查询返回的图形元素只是“引用”——意思是,它们只包含 id 和 label 而没有属性。您应该使用像 elementMap() 这样的步骤将结果转换为使用像 Map 这样的通用容器。您可以在各个地方的文档中找到关于此的更多讨论,但也许从 this and if you are interested more in why this is the way it is and what challenges are involved in changing it, please see this.

开始