TypeError when using and() in where()

I am trying to update based on two conditions. As far as I understand the docs, this should work, but I am getting a TypeError that I just can't get my head around. The code is even working, but since there is the TypeError, I can't build a production bundle. Consider this piece of code:
import { db } from ".";
import { apps } from "./schema";
import { auth } from "@clerk/nextjs/server";
import { and, eq } from "drizzle-orm";
import { generateAccessKey } from "~/lib/utils";

export async function resetAccessKeyForApp(appId: string) {
const user = auth();

try {
await db
.update(apps)
.set({ accessKey: generateAccessKey() })
.where(and(eq(apps.id, appId), eq(apps.user, user.userId)));
return { message: "success" };
} catch (e) {
console.log(e);
return { message: "Failed to reset access key." };
}
}
import { db } from ".";
import { apps } from "./schema";
import { auth } from "@clerk/nextjs/server";
import { and, eq } from "drizzle-orm";
import { generateAccessKey } from "~/lib/utils";

export async function resetAccessKeyForApp(appId: string) {
const user = auth();

try {
await db
.update(apps)
.set({ accessKey: generateAccessKey() })
.where(and(eq(apps.id, appId), eq(apps.user, user.userId)));
return { message: "success" };
} catch (e) {
console.log(e);
return { message: "Failed to reset access key." };
}
}
See the Screenshot for the TS Errpr. The problematic part seems to be the second eq: Any idea what I'm missing here?
No description
3 Replies
Sillvva
Sillvva•7mo ago
apps.user is not nullable and user.userId is string | null. Recommendation would be to throw or return early if user.userId is null, then type narrowing will ensure only string is passed to the query.
Philipp Nowinski
Philipp NowinskiOP•7mo ago
Oh boy. Thanks a lot, that's it. Guess what, all other actions have an early return for that case, didn't see the difference 😆
Sillvva
Sillvva•7mo ago
Yeah, that error is due to a type mismatch between the column and the input.
Want results from more Discord servers?
Add your server