#Actix - Web SQlite3

24 messages Β· Page 1 of 1 (latest)

river rune
#
pub async fn post_signin(tmpl: web::Data<Tera>, session: Session , form : web::Form<SigninUser> , conn: web::Data<sqlx::SqlitePool>) -> Result<HttpResponse, Error>{
    let mut ctx = Context::new();
    let add_user = sqlx::query("insert into users ( name , email , password ) values ( $1 , $2 , $3)")
        .bind(&form.name)
        .bind(&form.email)
        .bind(&form.password).execute(&**conn).await.unwrap();
     Ok(redirct("/"))
        
}
#[derive(Debug, Deserialize, Validate)]
pub struct SigninUser {
    #[validate(length(min = 4))]
    name: String,
    #[validate(email)]
    email: String,
    #[validate(length(min = 5))]
    password: String,
}

DataBasee does not record any information , i don'y know what's problem

select * from users;
Output: Nothing

meager rapids
# river rune ```rs pub async fn post_signin(tmpl: web::Data<Tera>, session: Session , form : ...

I think I know the issue and I would like to suggest committing the changes you made like for example if you have the connection object named as db then just add in the above code as db.commit() at the end of the changes you made. This will take all the changes you made from the memory and commit it to the database and then you would be able to see the changes you made. I hope this helps πŸ™‚

Also, for more information on the commit function I would suggest taking a look here:

https://docs.rs/sqlx/latest/sqlx/struct.SqliteConnection.html#method.commit

river rune
meager rapids
meager rapids
# river rune Isn't working

Ok, can provide me the new code and also if you got any recieved can you please attach with the new code. πŸ™‚

river rune
# meager rapids Ok, can provide me the new code and also if you got any recieved can you please ...
pub async fn post_signin(tmpl: web::Data<Tera>, session: Session , form : web::Form<SigninUser> , conn: web::Data<sqlx::SqlitePool>) -> Result<HttpResponse, Error>{
    let mut ctx = Context::new();
    let add_user = sqlx::query("insert into users ( name , email , password ) values ( $1 , $2 , $3)")
        .bind(&form.name)
        .bind(&form.email)
        .bind(&form.password).execute(&**conn).commit().await.unwrap();
    let a = tmpl.render("/",&ctx).map_err(error::ErrorInternalServerError)?;
    Ok(HttpResponse::Ok().body(a))
        
}
    
#

This is code πŸ˜„

meager rapids
# river rune ```rs pub async fn post_signin(tmpl: web::Data<Tera>, session: Session , form : ...

I would suggest trying this out and let me know if it works or not πŸ™‚ .

pub async fn post_signin(tmpl: web::Data<Tera>, session: Session , form : web::Form<SigninUser> , conn: web::Data<sqlx::SqlitePool>) -> Result<HttpResponse, Error>{
    let mut ctx = Context::new();
    let add_user = sqlx::query("insert into users ( name , email , password ) values ( $1 , $2 , $3)")
        .bind(&form.name)
        .bind(&form.email)
        .bind(&form.password).execute(&**conn).await.unwrap();
conn.commit();
    let a = tmpl.render("/",&ctx).map_err(error::ErrorInternalServerError)?;
    Ok(HttpResponse::Ok().body(a))
        
}
    
river rune
meager rapids
#

I think the execute function expects a mutable connection object but you are passing a immutable type maybe that's what is causing the issue I guess πŸ™‚. Here is an example I found in their tests which might be helpful in spotting the error, Notice the mutable references in the execute function. So I would suggest trying that out and let me know πŸ™‚.

Here is the link to the tests:

https://github.com/launchbadge/sqlx/blob/e80291b2a771bf66e9c03f068fd196c7a47c1967/tests/sqlite/sqlite.rs#L683

river rune
#

If you've got a time can you join Voice Channel

#

I share a screen

meager rapids
# river rune I share a screen

Sorry, I was a little busy so I was not able to reply. I think I might be free tomorrow so let's have a meeting tomorrow or if it is urgent then just DM me I would be glad to help πŸ™‚ .

river rune
meager rapids
river rune
#
#[derive(Debug, Deserialize, Validate)]
pub struct SigninUser{
    #[validate(length(min=5))]
    name: String,
    #[validate(email)]
    email: String,
    #[validate(length(min=6))]
    password: String,
    // password2: String
}
#
pub async fn post_signin(
    _tmpl: web::Data<Tera>,
    form: web::Form<SigninUser>,
    session: Session,
    conn: web::Data<sqlx::SqlitePool>) -> Result<HttpResponse, Error>{
    //let ctx = Context::new();
    let user = form.into_inner();
    if let Ok(_) = user.validate(){
        let add_user = sqlx::query("insert into users (name, email, password) values($1,$2,$3)")
            .bind(&user.name)
            .bind(&user.email)
            .bind(&bcrypt::hash(&user.password, DEFAULT_COST).expect("Şifreleme hatalı")).execute(&**conn).await;
        match add_user{
            Ok(_) => {
                session.insert("user", &user.name)?;
                // return Ok(redirect("/index.html"))
            }
            Err(_) => {
                return Ok(redirect("/signin"))
            }
        }
    }
    Ok(redirect("/signin"))
}
#
            <form action="/signin" method="post">
                <h1> Do You Ready To Fly ? </h1>
                <h1 class=" H1 "> Name </h1>
                <textarea class="Name-SignUp" name="Name-SignUp" id="" cols="30" rows="3"></textarea>
                <br><br>
                <h1 class=" H1 "> Email </h1>
                <textarea class="Email-SignUp" name="Email-SignUp" id="" cols="30" rows="3"></textarea>
                <br><br>
                <h1 class=" H1 "> Password</h1>
                <textarea class="Password-SignUp" name="Password-SignUp" id="" cols="30" rows="3"></textarea>
                <button> SignUp </button>
            </form>
meager rapids
# river rune ```html <form action="/signin" method="post"> <h1> D...

Ok found the problem, the error seems to occur because the actix-web form extractors uses the name attribute to serialize and deserialize data so you would need to update the form something like this to match the names provided in the structs which would look something like this:

<form action="/signin" method="post">
  <h1>Do You Ready To Fly ?</h1>
  <h1 class="H1">Name</h1>
  <input type="text" class="Name-SignUp" name="name" placeholder="Username" />
  <br /><br />
  <h1 class="H1">Email</h1>
  <input
    type="email"
    class="Email-SignUp"
    name="email"
    placeholder="Email Address"
  />
  <br /><br />
  <h1 class="H1">Password</h1>
  <input
    type="password"
    class="Password-SignUp"
    name="password"
    placeholder="Password"
  />
  <button type="submit">SignUp</button>
</form>

So I would suggest making the change to the form and this should be able to work now πŸ™‚ .

Note: I have made some improvements to the previous form that you have provided.

river rune