Uche Ozoemena
Uche Ozoemena

Reputation: 926

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?

Upvotes: 0

Views: 235

Answers (1)

Uche Ozoemena
Uche Ozoemena

Reputation: 926

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());

Upvotes: 1

Related Questions