D1 Issues

called `Result::unwrap()` on an `Err` value: Error(JsValue(Error:
invalid type: floating point `2.0`, expected a string
Error: invalid type: floating point `2.0`, expected a string
called `Result::unwrap()` on an `Err` value: Error(JsValue(Error:
invalid type: floating point `2.0`, expected a string
Error: invalid type: floating point `2.0`, expected a string
???
8 Replies
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
elpupper
elpupperOP2w ago
#[derive(Deserialize, Serialize, Debug)]
struct Credentials {
username: String,
password: String,
}

struct AppState {
d1: D1Database,
jwt_secret: String
}

async fn login(mut req: Request, ctx: RouteContext<AppState>) -> Result<Response> {
let creds: Credentials = req.json().await?;
tracing::info!("Credentials: {:#?}", creds);
let d1 = ctx.data.d1;

tracing::info!("Get User");
let result = d1
.prepare("SELECT * FROM users WHERE username = ? AND password = ?")
.bind(&[creds.username.into(), creds.password.into()])
.unwrap()
.run()
.await?;

match result.results::<User>() {
Ok(users) => {
tracing::info!("User Found");
let exp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs() as usize + 3600; // 1 hour expiration

tracing::info!("Generate JWT");

let claims = Claims {
sub: users[0].id.to_string(),
exp,
};
...
#[derive(Deserialize, Serialize, Debug)]
struct Credentials {
username: String,
password: String,
}

struct AppState {
d1: D1Database,
jwt_secret: String
}

async fn login(mut req: Request, ctx: RouteContext<AppState>) -> Result<Response> {
let creds: Credentials = req.json().await?;
tracing::info!("Credentials: {:#?}", creds);
let d1 = ctx.data.d1;

tracing::info!("Get User");
let result = d1
.prepare("SELECT * FROM users WHERE username = ? AND password = ?")
.bind(&[creds.username.into(), creds.password.into()])
.unwrap()
.run()
.await?;

match result.results::<User>() {
Ok(users) => {
tracing::info!("User Found");
let exp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs() as usize + 3600; // 1 hour expiration

tracing::info!("Generate JWT");

let claims = Claims {
sub: users[0].id.to_string(),
exp,
};
...
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
elpupper
elpupperOP2w ago
i dont see either but. i added a bit more context after... i have some tracing in there but its not getting logged
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
elpupper
elpupperOP2w ago
i think ur right
#[derive(Deserialize, Serialize)]
struct User {
id: String,
username: String,
password: String,
}
#[derive(Deserialize, Serialize)]
struct User {
id: String,
username: String,
password: String,
}
ok it works now i shoulda set the id to u32 thanks Rookie mistake from my side
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
elpupper
elpupperOP2w ago
Yeah but should always match the same type that u have in the schema but these silly mistakes make u lose ur mind with these errors.
Want results from more Discord servers?
Add your server