#This page isn't working (API redirect)
11 messages · Page 1 of 1 (latest)
bump
.
.
.
can you give some more context? potentially a public reproduction or a repl on replit.com or something else so people can effectively help you?
import clientPromise from "../../lib/mongodb";
export default async function handler(req, res) {
if (!req.query?.customerId) res.redirect(404, `https://${req.headers.host}/`);
const customerId = req.query.customerId;
if (req.method === "POST") {
try {
const client = await clientPromise;
const db = client.db("...");
const userDocument = await db
.collection("users")
.findOne({ customerId: customerId });
if (userDocument.questionsId !== "")
res.redirect(404, `https://${req.headers.host}/`);
await db.collection("...").insertOne({
...
});
await db.collection("...").updateOne(
...
);
res.redirect(300, `https://${req.headers.host}/`);
} catch (err) {
res.redirect(404, `https://${req.headers.host}/`);
}
} else {
res.setHeader("Allow", "POST");
res.status(405).end("Method Not Allowed");
}
}
im trying basically to submit the form then redirect the client to the index page with status code of 300
all the code executes as expected... but not the redirect
you have a lot of surrounding code and a db and you're awaiting stuff, no one can reproduce exactly what you're looking at without having a similar setup. I would recommend you add some logging to see if the line of code you're expecting to run is even getting hit and if you want any specific advice, make it easier for people to see exactly what you're seeing.
Im getting back the 300 status code back... just no redirect back