3

If I can check the value of a property like this: g.V().hasLabel('appUser').has('myId','1234').values('isPrivate') ==>false

But when I check if that value is false within an if, doesn't return what I would expect: if(g.V().hasLabel('appUser').has('myId','1234').values('isPrivate') == 'false'){'is false'}else{'is true'} ==>is true

Similarly, this also doesn't return what I would expect: if(!g.V().hasLabel('appUser').has('myId','1234').values('isPrivate')){'is false'}else{'is true'} ==>is true

How should I update this to perform a conditional check?

Fook
  • 5,320
  • 7
  • 35
  • 57

1 Answers1

2

.values() returns a Traversal, which is an Iterable (see TinkerPop 3.1.1 JavaDoc). You have to call .next() in order to actually retrieve the property value.

The provided query can be written like this.

if(!g.V().hasLabel('appUser').has('myId','1234').values('isPrivate').next()){'is false'}else{'is true'}

Notice the call to .next() right after .values('isPrivate').

When using the Gremlin Console, Traversal objects are automatically iterated (.iterate()), so what looks magical actually isn't. Mid-script (or outside of the Gremlin Console), you have to .next() or .iterate() the Traversal yourself in order to execute it, whether that Traversal is meant to retrieve elements (like the current use case) or mutate the graph.

jbmusso
  • 3,436
  • 1
  • 25
  • 36