can't resolve 'net'
Answered
West Siberian Laika posted this in #help-forum
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
West Siberian LaikaOP
I made this simple button with the only purpose of deleting a product, but it breaks my project whenever I import it to my admin page.
this error seems to be related to my
All related code (I use app router):
"use client";
I call the button in this component:
this error seems to be related to my
postgres
package which I use to interact with my supabase db.All related code (I use app router):
src/components/ui/dashboard/delete-button.tsx
"use client";
import { deleteProduct } from "@/lib/actions";
import { Button } from "../button";
import { useState } from "react";
export const DeleteProduct = (props: any) => {
const [error, setError] = useState(null);
const id = props.id;
async function handleDelete(e: any) {
const result = await deleteProduct(id);
if (!result) {
setError(e.error);
}
}
return <Button onClick={() => handleDelete} />;
};
src/lib/actions.ts
import { revalidatePath } from "next/cache";
import sql from "../../db";
export async function deleteProduct(id: any) {
try {
const product = await sql`
delete from products
where id = ${id};
`;
revalidatePath("/admin");
return product;
} catch (e) {
console.log("Error deleting product: ", e);
throw e;
}
}
I call the button in this component:
src/app/admin/page.tsx
import { DeleteProduct } from "@/components/ui/dashboard/delete-button";
import { Products } from "@/components/ui/dashboard/products";
import { getProducts } from "@/lib/actions";
export default async function Page() {
const products = await getProducts(100);
return (
{products?.map((product) => (
<>
<Products
title={product.title}
imageurl={product.imageurl}
price={1000}
/>
<DeleteProduct id={product.id} />
</>
))}
);
}
![Image](https://cdn.discordapp.com/attachments/1240354228313260062/1240354228807929896/Screenshot_371.png?ex=66464164&is=6644efe4&hm=3a238a6e30548daa0d7a0cd7c50470580cbf730502a3dfe5afb2917c151f9c8f&)
15 Replies
![Avatar](https://cdn.discordapp.com/avatars/484037068239142956/44415017de6fed2ccd5d44b4b2051ac0.webp?size=256)
joulev
Add "use server" at the top of the actions.ts file
Answer
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
West Siberian LaikaOP
Wow, I searched many forums but no one suggested that, what was the cause of this? I thought Nextjs components are server components by default.
![Avatar](https://cdn.discordapp.com/avatars/890486507872342027/cba9267c0173c6acfde7fa4ba0e70f94.webp?size=256)
Arinji
Server components arent the same as server actions.
![Avatar](https://cdn.discordapp.com/avatars/484037068239142956/44415017de6fed2ccd5d44b4b2051ac0.webp?size=256)
joulev
delete-button is a client component, so you were asking your app to run the sql query on the browser which obviously doesn’t work
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
West Siberian LaikaOP
Thank you so much guys @joulev @Arinji I was also gonna ask you why my button wasn't working but I realized I wasn't calling the function properly lol
![Avatar](https://cdn.discordapp.com/avatars/890486507872342027/cba9267c0173c6acfde7fa4ba0e70f94.webp?size=256)
Arinji
No worries, mark a solution :D
boop
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
West Siberian LaikaOP
ofc, I'll also give it a better name
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Giant panda
import { deleteProduct } from "@/lib/actions";
import { Button } from "../button";
import { useState } from "react";
export const DeleteProduct = (props: any) => {
const [error, setError] = useState(null);
const id = props.id;
async function handleDelete(e: any) {
const result = await deleteProduct(id);
if (!result) {
setError(e.error);
}
}
return <Button onClick={() => handleDelete} />;
};
why you are passing the function like that when it doesn't have params![:meow_stare:](https://cdn.discordapp.com/emojis/763826437341839422.png)
![Avatar](https://cdn.discordapp.com/embed/avatars/3.png)
West Siberian LaikaOP
My coding philosophy, write first, think later lol
"use client";
import { deleteProduct } from "@/lib/actions";
import { Button } from "../button";
import { useState } from "react";
export default function DeleteProduct(props: any) {
const [error, setError] = useState(null);
const id = props.id;
async function handleDelete(e: any) {
const result = await deleteProduct(id);
if (!result) {
setError(e.error);
}
}
return (
<Button variant={"destructive"} onClick={() => handleDelete(id)}>
Delete
</Button>
);
}
just added parenthesis to click event
It works so I probably won't touch it for the next 10 light years
![Avatar](https://cdn.discordapp.com/embed/avatars/2.png)
Giant panda
great