rethinkdb / rethinkdb-rs

A native RethinkDB driver written in Rust
Apache License 2.0
210 stars 27 forks source link

client error; invalid type: map, expected a string while reading from a stream #62

Closed jmwielandt closed 3 years ago

jmwielandt commented 3 years ago

Hi, I'm learning how to use RethinkDB and I ran into a weird error.

Code:

use reql::cmd::connect::Options;
use reql::{r, Error, Session};
use futures::stream::{StreamExt};

const DB_NAME: &str = "my_custom_db";

#[tokio::main]
async fn main() -> Result<(), Error> {
    let mut cfg = Options::default();
    cfg = cfg.db(DB_NAME);
    cfg = cfg.host("localhost");
    cfg = cfg.port(28015);

    println!("Starting...");
    let db = r.connect(cfg).await?;
    println!("Connected!");

    let mut result = r.db_create(DB_NAME).run::<&Session, String>(&db);
    while let Some(x) = result.next().await {
        match x {
            Ok(y) => println!("SUCCESS: {}", y),
            Err(e) => println!("ERROR: {}", e),
        }
    }

    let mut result = r.table_create("users").run::<&Session, String>(&db);
    while let Some(x) = result.next().await {
        match x {
            Ok(y) => println!("SUCCESS: {}", y),
            Err(e) => println!("ERROR: {}", e),
        }
    }

    db.connection()?.close(()).await?;
    Ok(())
}

The output:

Starting...
Connected!
ERROR: client error; invalid type: map, expected a string
ERROR: client error; invalid type: map, expected a string

And the weird part: the database and table are being created: image

Am I doing something wrong? Thanks!

rushmorem commented 3 years ago

Hi,

These error messages are coming from serde. It's failing to deserialize the response from the database to the type you are expecting, String. If you look at RethinkDB API docs for the official drivers you will see that both db_create and table_create return an object not a string.

Whenever you are not sure of the database response type or you don't care, you should use serde_json::Value instead. Also if you enable logging you will see what the database is returning before the driver tries to deserialize it. See https://github.com/rethinkdb/rethinkdb-rs/blob/5eac333bdb54d451f9964486d4361fdc71056f68/examples/reql/changes.rs for an example that showcases both.