Next.js Discord

Discord Forum

How to force a 404 in a page.js? (using app directory)

Answered
Marble gall posted this in #help-forum
Open in Discord
Marble gallOP
i have the code below and i want to redirect to my 404 error page if the array of animeResult.items is empty, but i can't seem to get a res object from my server page's constructor, how do i do this?
import getAnimeByName from "@/app/data/getAnimeByName"
import pb from "@/app/data/pocketBase"
import Image from "next/image"

export default async function WatchAnime({ props, params }, res) {
    let animeName = params.name
    let animeResult = await getAnimeByName(animeName)
    let anime = animeResult.items[0]

    if (!animeResult || animeResult.items.length || !anime) {
        return res.status(404)
    }

    return <div className="container-fluid mx-0 px-0 row fw-bold mt-3 fw-boldest">
        <div className="col-12 col-md-8">
            <h4 className="fw-boldest text-light">
                {anime.animeTitle}
            </h4>
        </div>
......more frontend etc.

the code above at the moment throws an Error: Cannot read properties of undefined (reading 'status') at res.status, res seems null or undefined

5 Replies