Next.js Discord

Discord Forum

Server function not saving data to database

Answered
Brittany posted this in #help-forum
Open in Discord
BrittanyOP
I created a cron job to web scrap a webpage, but it just happens at build time. In the log shows it's triggering the function every 5 minutes, like it's supposed to do, but when I check the database, nothing happened.

File path: app/api/crons/getPlayersOnline/route.ts
import * as cheerio from "cheerio" import { prisma } from '@/app/libs/dbClient' type PlayerStats = { name: string vocation: string level: number } export async function GET() { const URL = "https://tibiantis.online/?page=whoisonline" try { const response = await fetch(URL, { cache: 'no-store', next: { revalidate: 0 } }) const htmlString = await response.text() const $ = cheerio.load(htmlString) const playersOnline: PlayerStats[] = [] $('.hover').each((i, tr) => { const playerStats: PlayerStats = { name: '', vocation: '', level: 0 } $(tr).find("td").each((i, td) => { switch (i) { case 0: playerStats.name = $(td).text() break case 1: playerStats.vocation = $(td).text() break case 2: playerStats.level = Number($(td).text()) break } }) playersOnline.push(playerStats) }) await prisma.playersOnline.deleteMany({}) await prisma.playersOnline.createMany({ data: playersOnline }) await prisma.playersHistory.create({ data: { quantity: playersOnline.length } }) return Response.json({ message: "Players online loaded." }, { status: 200 }) } catch (err: any) { console.error(err.message, err.code) return Response.json({ message: "Could not load players online." }, { status: 500 }) } }
Answered by aardani
try putting export const dynamic = 'force-dynamic' in ur route.ts file
View full answer

114 Replies

try console.log(playersOnline) and see if the array is empty or not
whats the response of the call?
BrittanyOP
If i do it on my pc on dev, it works fine and returns the players. But once I deploy, it stores the data just once and it stops, but I don't know how to check it on vercel page
I'm using pro plan, I don't know if that helps
BrittanyOP
don't show nothing there, i tried already
i'm even triggering the cron job by myself
well does any of the console.log logs anything?
BrittanyOP
yes
is your db environment variables properly set up?
what about the build logs?
did you have prisma generate command on the build task of the Vercel project?
BrittanyOP
yes i did it already in my prisma
its also working the connection on dev mode on my pc
i can write and delete on db anytime just going to localhost:3000/api/crons/getPlayersOnline
and its working fine
but the cron isnt?
BrittanyOP
everytime it stores on the db
yes therefore i ask whether or not the db envrionment variables are properly set up in your deployment
also if you have prisma generate command on the build task of the vercel project when its deployed?
BrittanyOP
yes i changed it to
"scripts": {
"dev": "next dev",
"build": "prisma generate && next build",
"start": "next start",
"lint": "next lint"
},
on deployment i dont have access, because its an api route
i just can do it on dev mode, when im hosting on my pc
you have access to an api route.. werent you given a link?
BrittanyOP
but im triggering the cron job on settings => cron jobs => run
but isnt it internal?
cause its an route.ts
not page.ts
no?
route.ts are public apis
tibiantis-club-something.vercel.app/api/crons/getPlayersOnline
?
BrittanyOP
i have a domain to make it easy
tibiantis.club
you are right
i can trigger it
its private if you do this
but
thats beside the main problem :''
BrittanyOP
yea ill try to hide the api after i solve this
but anyway, i could trigger it from the url
does it change the db?
is there any warnings in the build logs?
BrittanyOP
no warnings
im checking db
click any of these and see any warnings/logs are outputted?
BrittanyOP
not chaing
oh
status code 034
304
seems that its not modified
304 from where?
from the db?
BrittanyOP
from getPlayersOnline function
i clicked on the get function u told me to do
but ur function only returns 200 and 500 :v
BrittanyOP
yes but on the log u told me
let me print it
oh
maybe because i triggered from url?
no logs? scrolldown from the sidebar?
BrittanyOP
nothing
it just triggered again, and its 200
i think i got 304 from triggering manually from tibiantis.club/api/crons/getPlayersOnline
none of them changed db
the console logs are also not getting printed
isnt it?
BrittanyOP
i think its not
i inserted a console.log inside the function
but its not showing nothing
try
export async function GET() {
  console.log("Is this printed?")    
BrittanyOP
that's exactly what i did
may i see your vercel.json?
BrittanyOP
sure
{
"crons": [
{
"path": "/api/crons/getPlayersOnline",
"schedule": "*/5 * * * *"
}
]
}
ah
hmm
try putting export const dynamic = 'force-dynamic' in ur route.ts file
Answer
BrittanyOP
ok
should i keep this? const response = await fetch(URL, { cache: 'no-store', next: { revalidate: 0 } })
sure, lets try all the things
BrittanyOP
ok
console.log now its printed
:O
BrittanyOP
it says that i have to choose now "etch for https://tibiantis.online/?page=whoisonline on /api/crons/getPlayersOnline specified "cache: no-store" and "revalidate: 0", only one should be specified."
which one i choose?
any is fine
both means the same thing
BrittanyOP
ok
im deploying agian
just for context, the last warning was like this
niiiiiiice
its working right now
im triggering it from the url like u told me
ill wait for cron job
it's working fine
is this a bug?
the cron job triggered by itself, its working
thank you so much alfonsus
@Brittany is this a bug?
its a common pitfall :D
GET routes are statically computed by default
so it only runs once at build time and the result will be cached for the next request
Next.js doesn't know that you want to run that for every request i guess
BrittanyOP
thank you again
can u send me the docs where i can hide my api?