Next.js Discord

Discord Forum

server action in client

Unanswered
Gharial posted this in #help-forum
Open in Discord
GharialOP
Hi, there is my next on client side and i don't understand a concept of getting my getLinkStatistics() because of a
Unhandled Runtime Error

Error: ❌ Attempted to access a server-side environment variable on the client

caused by my next server action because the next server action use the auth for getting the user etc so what can i do in use client ? bcz i need use a zustand
:
"use client";

export default function StatsVisits() {

    const [stats, setStats] = useState({
      uniqueVisitorsCurrent: null,
      totalVisitsCurrent: null,
      uniqueVisitorPercentageChange: null,
      totalVisitPercentageChange: null,
      uniqueVisitorTrend: null,
      totalVisitTrend: null,
    });
  
    // zustand part
    useEffect(() => {
      
      async function fetchAndSetStats() {
        const fetchedStats = await getLinkStatistics({here is my zustand etc});
        setStats(fetchedStats);
      }
      fetchAndSetStats();
    }, []);

    return (
      <div className="max-w-[85rem] mx-auto">
        <div>Visiteurs uniques actuels : {stats.uniqueVisitorsCurrent}</div>
        <div>Visites totales actuelles : {stats.totalVisitsCurrent}</div>
        <div>Changement en pourcentage des visiteurs uniques : {stats.uniqueVisitorPercentageChange}%</div>
        <div>Changement en pourcentage des visites totales : {stats.totalVisitPercentageChange}%</div>
        <div>Tendance des visiteurs uniques : {stats.uniqueVisitorTrend}</div>
        <div>Tendance des visites totales : {stats.totalVisitTrend}</div>
      </div>
    );

3 Replies

@Gharial Hi, there is my next on client side and i don't understand a concept of getting my `getLinkStatistics()` because of a ls Unhandled Runtime Error Error: ❌ Attempted to access a server-side environment variable on the client caused by my next server action because the next server action use the `auth` for getting the user etc so what can i do in use client ? bcz i need use a zustand : ts "use client"; export default function StatsVisits() { const [stats, setStats] = useState({ uniqueVisitorsCurrent: null, totalVisitsCurrent: null, uniqueVisitorPercentageChange: null, totalVisitPercentageChange: null, uniqueVisitorTrend: null, totalVisitTrend: null, }); // zustand part useEffect(() => { async function fetchAndSetStats() { const fetchedStats = await getLinkStatistics({here is my zustand etc}); setStats(fetchedStats); } fetchAndSetStats(); }, []); return ( <div className="max-w-[85rem] mx-auto"> <div>Visiteurs uniques actuels : {stats.uniqueVisitorsCurrent}</div> <div>Visites totales actuelles : {stats.totalVisitsCurrent}</div> <div>Changement en pourcentage des visiteurs uniques : {stats.uniqueVisitorPercentageChange}%</div> <div>Changement en pourcentage des visites totales : {stats.totalVisitPercentageChange}%</div> <div>Tendance des visiteurs uniques : {stats.uniqueVisitorTrend}</div> <div>Tendance des visites totales : {stats.totalVisitTrend}</div> </div> );
American
do not use "use server" in the same level with useEffect, create a new file and write your server action and import to client component
@American do not use "use server" in the same level with useEffect, create a new file and write your server action and import to client component
GharialOP
getLinkStatistics is my server actions that i have in another file and i imported but can you help me for getting it in client side ?