Dominik Scherm
Dominik Scherm

Reputation: 33

How to fix Google Firestore issue in Elixir?

I always run into an issue, when calling: GoogleApi.Firestore.V1beta1.Api.Projects.firestore_projects_databases_documents_run_query(), as shown below.

I already tried different inputs and formatting ways, as well as different firestore collections.

{:ok, token} = Goth.Token.for_scope("https://www.googleapis.com/auth/cloud-platform")

conn = GoogleApi.Firestore.V1beta1.Connection.new(token.token)

query = %GoogleApi.Firestore.V1beta1.Model.RunQueryRequest{
  structuredQuery: %{
    :from => %{:collectionId => "demo"},
    :limit => 1
  }
}

GoogleApi.Firestore.V1beta1.Api.Projects.firestore_projects_databases_documents_run_query(
  conn,
  "projects/demo/databases/(default)/documents",
  body: query
)

Result:

** (BadMapError) expected a map, got: [%{"readTime" => "2019-04-07T20:23:48.219664Z"}]
    (elixir) lib/map.ex:437: Map.get([%{"readTime" => "2019-04-07T20:23:48.219664Z"}], "document", nil)
    lib/poison/decoder.ex:53: anonymous fn/3 in Poison.Decode.transform_struct/4
    (stdlib) maps.erl:257: :maps.fold_1/3
    lib/poison/decoder.ex:52: Poison.Decode.transform_struct/4
    lib/poison.ex:70: Poison.decode/2

Upvotes: 3

Views: 551

Answers (1)

yorbro
yorbro

Reputation: 1137

For what it's worth, I've managed to load a document by doing the following:

{:ok, token} = Goth.Token.for_scope("https://www.googleapis.com/auth/datastore")
conn = GoogleApi.Firestore.V1.Connection.new(token.token)
{:ok, document} = GoogleApi.Firestore.V1.Api.Projects.firestore_projects_databases_documents_get(
  conn,
  "projects/{project_id}/databases/(default)/documents/{collection_id}/{document_id}"
)

where the variables {XXX} are replaced with the actual values.

Maybe not exactly your use case but since examples of using Firestore in Elixir are hard to find, this might be useful to someone!

Upvotes: 3

Related Questions