Argument cannot be used as lookup term

I’m trying to query data from multiple collections but end up getting an error.

This is the query

let actors = Actors.byUserId("foo").pageSize(50)
let actor = actors.firstWhere(item => item.slug == "bar")
let projects = Projects.byActorId(actor.id)

{
  actors: actors,
  actor: actor,
  projects: projects,
}

I also tried changing line 3

let projects = Projects.byActorId(actor?.id)
let projects = Projects.byActorId(actor!.id)

These are the errors I am getting for the various combinations

invalid_query

error: Type `Null` does not have field `id`
at *query*:4:27
  |
4 | if (actor!= null && actor.id != null) {
  |                           ^^
  |
hint: Use the ! or ?. operator to handle the null case
  |
4 | if (actor!= null && actor!.id != null) {
  |                          +
  |

error: Type `Null` does not have field `id`
at *query*:5:43
  |
5 |   let projects = Projects.byActorId(actor.id)
  |                                           ^^
  |
hint: Use the ! or ?. operator to handle the null case
  |
5 |   let projects = Projects.byActorId(actor!.id)
  |                                          +
  |
invalid_argument

error: invalid argument `term1`: Argument cannot be used as lookup term
at *query*:3:34
  |
3 | let projects = Projects.byActorId(actor!.id)
  |                                  ^^^^^^^^^^^
  |
invalid_argument

error: invalid argument `term1`: Argument cannot be used as lookup term
at *query*:3:34
  |
3 | let projects = Projects.byActorId(actor?.id)
  |                                  ^^^^^^^^^^^
  |

Hi @dferber and welcome! :wave:

I think that the issue is that actor!.id will be of type ID, and ID values cannot be persisted. Therefore we know without any other checks that you cannot use it for an index lookup and you get the following.

It would be much nicer if the error would explain that the issue is the ID type (assuming that’s the issue).

Can you try actor!.id.toString()? This will ensure that the term provided is non-null AND a string value.


As an aside, I would recommend indexing on the document references, rather than the ID. This can make traversing relationships and using references for indexes easier.

for example:

// FSL
collection Actors { /* ... */ }

collection Projects {
  actor: Ref<Actor> // persist the whole reference, not just the stringified ID

  index byActor {
    terms [.actor]
  }
}
// FQL
let actor = actors.firstWhere(item => item.slug == "bar")! // you can assert non-null here
let projects = Projects.byActorId(actor) // pass in the reference to the index call

// -- or --
Projects.byId("1234") {
  id,
  actor { // since we store the reference, we can project this field directly as a document 
    id,
    // ...
  }
}