#Unexpected result type: [DecodeError("Tuple of 2 elements", "Tuple of 1 elements", [])]

1 messages · Page 1 of 1 (latest)

hallow light
#

@limpid folio whenever you're free take a look wavefoxy

limpid folio
#

Could you use rs as the code block's language to get some syntax highlight?

#

What's the query that's resulting in a failure here?

quick turtle
#

must be get_count right?

#

I think you need to change

dynamic.tuple2(
        dynamic.int,
        dynamic.dynamic
      )(dyn)
      |> result.map(fn(tuple) { tuple.0 })

to
dynamic.element(0, dynamic.int)

#

so im guessing the problem is that the query only returns one thing but youre tyring to decode two things with tuple2

hallow light
#

alright

#

wait

hallow light
#

discord messages are so laggy ugh

quick turtle
hallow light
#
    expecting: fn(dyn) {
      dynamic.element(0,dynamic.dynamic)(dyn)
      |> result.map(fn(tuple) { tuple.0 })
    }
#

can something like this work ? @quick turtle

limpid folio
#

I don't think this will type check

#

dynamic.element(0, dynamic.dynamic) returns a Result(Dynamic, _) and you can't index into that tuple

#

What are you trying to do?

hallow light
#
import gleam/result
import gleam/string
import gleam/pgo
import gleam/io
import gleam/dynamic
import gleam/int

pub fn table(conn: pgo.Connection) -> Result(Nil, String) {
  let create_table_query = "
    CREATE TABLE IF NOT EXISTS entries (
      id SERIAL PRIMARY KEY,
      created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
    )
  "
  io.println("Ensuring entries table exists...")
  case pgo.execute(
    query: create_table_query,
    on: conn,
    with: [],
    expecting: dynamic.dynamic
  ) {
    Ok(_) -> {
      io.println("Table entries created or already exists")
      Ok(Nil)
    }
    Error(err) -> {
      let error_message = query_error_to_string(err)
      io.println("Failed to create table: " <> error_message)
      Error("Failed to create table: " <> error_message)
    }
  }
}

pub fn add_entry_and_get_count(conn: pgo.Connection) -> Result(Int, String) {
  io.println("Adding entry and getting count")
  
  case table(conn) {
    Ok(_) -> {
      let insert_query = "INSERT INTO entries (created_at) VALUES (NOW())"
      io.println("Executing insert query: " <> insert_query)
      case pgo.execute(
        query: insert_query,
        on: conn,
        with: [],
        expecting: dynamic.dynamic
      ) {
        Ok(_) -> {
          io.println("Entry added successfully")
          get_count(conn)
        }
        Error(err) -> {
          let error_message = query_error_to_string(err)
          io.println("Failed to add entry: " <> error_message)
          Error("Failed to add entry: " <> error_message)
        }
      }
    }
    Error(err) -> {
      io.println("Failed to ensure table exists: " <> err)
      Error("Failed to ensure table exists: " <> err)
    }
  }
}

fn get_count(conn: pgo.Connection) -> Result(Int, String) {
  let count_query = "SELECT COUNT(*) FROM entries"
  io.println("Executing count query: " <> count_query)
  
  case pgo.execute(
    query: count_query,
    on: conn,
    with: [],
    expecting: fn(dyn) {
      dynamic.element(0, dynamic.int)(dyn)
    }
  ) {
    Ok(result) -> {
      io.println("Raw result: " <> string.inspect(result))
      case result.rows {
        [count] -> {
          io.println("Count retrieved successfully: " <> int.to_string(count))
          Ok(count)
        }
        _ -> {
          io.println("Unexpected result format")
          Error("Unexpected result format")
        }
      }
    }
    Error(err) -> {
      let error_message = query_error_to_string(err)
      io.println("Failed to get count: " <> error_message)
      Error("Failed to get count: " <> error_message)
    }
  }
}

fn query_error_to_string(error: pgo.QueryError) -> String {
  case error {
    pgo.ConstraintViolated(message: message, constraint: constraint, detail: detail) ->
      "Constraint violated: " <> message <> " (Constraint: " <> constraint <> ", Detail: " <> detail <> ")"
    pgo.PostgresqlError(code: code, name: name, message: message) ->
      "PostgreSQL error: " <> message <> " (Code: " <> code <> ", Name: " <> name <> ")"
    pgo.UnexpectedArgumentCount(expected: expected, got: got) ->
      "Unexpected argument count. Expected: " <> int.to_string(expected) <> ", Got: " <> int.to_string(got)
    pgo.UnexpectedArgumentType(expected: expected, got: got) ->
      "Unexpected argument type. Expected: " <> expected <> ", Got: " <> got
    pgo.UnexpectedResultType(errors) ->
      "Unexpected result type: " <> string.inspect(errors)
    pgo.ConnectionUnavailable ->
      "Connection unavailable"
  }
}
``` well the point of all this is counting the number enteries(whenever a person sends a request to the page) and creates a entry's id  its still pending because i have to test the counting mechanism 😔
limpid folio
#

What are you trying to do with that? Why are you trying to write the decoder that way? What kind of data are you trying to decode?

hallow light
#

this is the function i have in my app.gleam fn ```rs
web_service(_request) {
io.println("Received a new request")

// Check database connection using config
let #(db_status, entry_count) = case config.get_db_connection() {
Ok(connection) -> {
io.println("Database connection successful")
// Use the database module to add entry and get count
case database.add_entry_and_get_count(connection) {
Ok(count) -> {
io.println("Successfully added entry and got count: " <> int.to_string(count))
#("Database connection successful! :white_check_mark:", count)
}
Error(err) -> {
io.println("Database operation failed: " <> err)
#("Database operation failed: " <> err, 0)
}
}
}
Error(err) -> {
let error_message = case err {
Nil -> "Unknown database connection error"
}
io.println(error_message)
#(error_message <> " :x:", 0)
}
}

#

dead

limpid folio
#

It's not in anywhere in your code so I can't really help you fixing it, sorry

quick turtle
hallow light
#

so ended up using gluid today

#

and decoding tuple's to the normal entries in integer format

#

and then later show on the web server html

hallow light
#
  let count_query = "SELECT COUNT(*) FROM entries"
  io.println("Executing count query: " <> count_query)
  case pgo.execute(
    query: count_query,
    on: conn,
    with: [],
    expecting: fn(row) {
      dynamic.element(0, dynamic.int)(row)
    }
  ) {
    Ok(result) -> {
      case result.rows {
        [count] -> {
          io.println("Count retrieved successfully: " <> int.to_string(count))
          Ok(count)
        }
        _ -> {
          io.println("Unexpected result format")
          Error("Unexpected result format")
        }
      }
    }
    Error(err) -> {
      let error_message = query_error_to_string(err)
      io.println("Failed to get count: " <> error_message)
      Error("Failed to get count: " <> error_message)
    }
  }
}``` this is what im using now and works perfectly fine
limpid folio
#

Yeah looks good to me!

hallow light
#

also

#

sorry i wasnt making sense before

#

thanks for your time

#

im a bit new LOL

limpid folio
#

No worries! We’ve all been there espeon

hallow light
#

Personally i really think i should work with sql more - im too rusty as far as i remember i used it 4 years ago in 9th grade

limpid folio
#

Yeah the nice thing about squirrel is that it doesn’t hide the SQL!

limpid folio
#

It’s just plain old .sql files minus the pgo boilerplate

hallow light
#

smuguwu alright i'll try to make a bigger project with gleam