简体   繁体   中英

create vertex if not exists with nodejs gremlin

I'm trying to use the technique described here for preventing duplicated edges in gremlin. I'm working in javascript and the query is failing with this error: Server error: Neither the map, sideEffects, nor path has a v-key: WhereEndStep(v) (500) . This is the exact query I'm using:

import { process } from "gremlin";
const { statics } = process;
...
            g
            .V()
            .has('user', 'id', this.id)
            .fold()
            .coalesce(statics.unfold(), statics
                    .addV('user')
                    .property('id', this.id))
            .as('v')
            .V()
            .has('user', 'id', anotherUserId)
            .fold()
            .coalesce(statics.unfold(), statics
                    .addV('user')
                    .property('id', anotherUserId))
            .coalesce(statics
                    .inE('follow')
                    .where(
                        statics.outV().as('v')
                    ), statics.addE('follow').from_('v'))
            .V()
            .has('id', this.id)
            .outE('skip')
            .where(statics.inV().has('id', anotherUserId))
            .drop()
            .toList());

That's for completeness but the query never reaches the final outE('skip') , it fails at the last coalesce that's meant to prevent the duplicated edge. What could I be missing please?

As someone pointed out in the comments, the problem was that labels do not persist across a .fold() . My solution was to break up the queries so I could preserve the relevant label. What worked was this:

import { process } from "gremlin";
const { statics } = process;
...
            // first make sure the second user is available
            g = g
            .V()
            .has('user', 'id', anotherUserId)
            .fold()
            .coalesce(statics.unfold(), statics
                    .addV('user')
                    .property('id', anotherUserId))

            // then add the edge after making sure the first user is available
            g
            .V()
            .has('user', 'id', this.id)
            .fold()
            .coalesce(statics.unfold(), statics
                    .addV('user')
                    .property('id', this.id))
            .as('v')
            .V()
            .has('user', 'id', anotherUserId)
            .coalesce(statics
                    .inE('follow')
                    .where(
                        statics.outV().as('v')
                    ), statics.addE('follow').from_('v'))
            .V()
            .has('id', this.id)
            .outE('skip')
            .where(statics.inV().has('id', anotherUserId))
            .drop()
            .toList());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM