Next.js Discord

Discord Forum

How to redirect?

Answered
Siamese posted this in #help-forum
Open in Discord
SiameseOP
useEffect(() => {
        Promise.all([
            axios.get(`/api/bot/guild?id=${id}`),
            axios.get(`/api/db/guild?id=${id}`)
        ]).then(function (response: Array<any>) {
            setData({
                bot: response[0]?.data,
                db: response[1]?.data
            });
        }).catch(error => {
            if (error.response.status === 401) redirect(error.response.data.redirect);
        });
    }, [id]);

this is my code, how can i fix the redirect? it gives some error with NEXT_REDIRECT and nothing more
Answered by joulev
use router.push instead. redirect is not to be used in this kind of situation
View full answer

6 Replies

Answer
@joulev use `router.push` instead. `redirect` is not to be used in this kind of situation
SiameseOP
any way to redirect directly on api route in this case?
would be easier to do it there rather than after fetch
@Siamese any way to redirect directly on api route in this case?
from api routes, no, impossible. if you use server action then yes redirect works there
SiameseOP
aight
thanks