quering with nuxt + drizzle + cloudflare D1

I'm using nuxt + drizzle orm + cloudflare D1. my schema.ts looks like this
import { sqliteTable, text, integer } from "drizzle-orm/sqlite-core"

export const user = sqliteTable('users', {
id: text("id").primaryKey(),
firstname: text("first_name").notNull(),
lastname: text("last_name").notNull(),
email: text("email").unique().notNull(),
username: text("username").unique().notNull(),
password: text("password").notNull(),
confirm_password: text("confirm_password").notNull(),
country: text("country").notNull(),
gender: text("gender").notNull(),
date_of_birth: text("date_of_birth").notNull(),
selectedPhoneCode: text("selectedPhoneCode").notNull(),
phone: text("phone").notNull(),
created_at: integer('created_at', { mode: 'timestamp' }).notNull()
})


export const session = sqliteTable("session", {
id: text("id").primaryKey(),
expiresAt: integer("expires_at", { mode: "timestamp" }).notNull(),
userId: text("user_id")
.notNull()
.references(() => user.id, { onDelete: "cascade" }),
})
import { sqliteTable, text, integer } from "drizzle-orm/sqlite-core"

export const user = sqliteTable('users', {
id: text("id").primaryKey(),
firstname: text("first_name").notNull(),
lastname: text("last_name").notNull(),
email: text("email").unique().notNull(),
username: text("username").unique().notNull(),
password: text("password").notNull(),
confirm_password: text("confirm_password").notNull(),
country: text("country").notNull(),
gender: text("gender").notNull(),
date_of_birth: text("date_of_birth").notNull(),
selectedPhoneCode: text("selectedPhoneCode").notNull(),
phone: text("phone").notNull(),
created_at: integer('created_at', { mode: 'timestamp' }).notNull()
})


export const session = sqliteTable("session", {
id: text("id").primaryKey(),
expiresAt: integer("expires_at", { mode: "timestamp" }).notNull(),
userId: text("user_id")
.notNull()
.references(() => user.id, { onDelete: "cascade" }),
})
1 Reply
KWAC
KWAC2mo ago
my api (api/login/signup.post.js) looks like this:
import { generateId } from "lucia";
import { user } from "@/server/db/schema.ts";

export default defineEventHandler(async (event) => {

const lucia = event.context.lucia;
const db = event.context.db;

const body = await readBody(event);

const {
firstname,
lastname,
email,
username,
password,
confirm_password,
country,
gender,
phone,
selectedPhoneCode,
date_of_birth,
terms
} = body;


try{
await db.insert(user).values({
id: userId,
firstname: firstname,
lastname: lastname,
email: email,
username: username,
password: password,
confirm_password: confirm_password,
country: country,
gender: gender,
date_of_birth: date_of_birth,
selectedPhoneCode: selectedPhoneCode,
phone: phone,
created_at: createdAt,
}).run();

const session = await lucia.createSession(userId, {});
appendHeader(event, "Set-Cookie", lucia.createSessionCookie(session.id).serialize());
return { success: true};
} catch (error) {
return { success: false, errors };
}
}

});
import { generateId } from "lucia";
import { user } from "@/server/db/schema.ts";

export default defineEventHandler(async (event) => {

const lucia = event.context.lucia;
const db = event.context.db;

const body = await readBody(event);

const {
firstname,
lastname,
email,
username,
password,
confirm_password,
country,
gender,
phone,
selectedPhoneCode,
date_of_birth,
terms
} = body;


try{
await db.insert(user).values({
id: userId,
firstname: firstname,
lastname: lastname,
email: email,
username: username,
password: password,
confirm_password: confirm_password,
country: country,
gender: gender,
date_of_birth: date_of_birth,
selectedPhoneCode: selectedPhoneCode,
phone: phone,
created_at: createdAt,
}).run();

const session = await lucia.createSession(userId, {});
appendHeader(event, "Set-Cookie", lucia.createSessionCookie(session.id).serialize());
return { success: true};
} catch (error) {
return { success: false, errors };
}
}

});
Here's a snippet from my signup.vue page making api call:
const signupData = {
firstname: this.firstnameSignUp,
lastname: this.lastnameSignUp,
email: this.emailSignUp,
username: this.usernameSignUp,
password: this.passwordSignUp,
confirm_password: this.confirm_password,
country: this.selectedCountry,
gender: this.gender,
phone: this.phone,
selectedPhoneCode: this.selectedPhoneCode,
date_of_birth: this.date_of_birth,
terms: this.terms
};

const response = await fetch('/api/login/signup', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(signupData)
});

const result = await response.json();

if (!result.success) {
this.errors = result.errors;
} else {
await navigateTo('/verify');
}
const signupData = {
firstname: this.firstnameSignUp,
lastname: this.lastnameSignUp,
email: this.emailSignUp,
username: this.usernameSignUp,
password: this.passwordSignUp,
confirm_password: this.confirm_password,
country: this.selectedCountry,
gender: this.gender,
phone: this.phone,
selectedPhoneCode: this.selectedPhoneCode,
date_of_birth: this.date_of_birth,
terms: this.terms
};

const response = await fetch('/api/login/signup', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(signupData)
});

const result = await response.json();

if (!result.success) {
this.errors = result.errors;
} else {
await navigateTo('/verify');
}
Data is not inserted into a table from api call. But from drizzle kit studio (using D1) I can modify data without problems. Am I doing something wrong here?
Want results from more Discord servers?
Add your server