I have this node.js code where I process a post request sent from fetch (ejs)
app.post('/signup', jsonParser, async function(req, res) {
const email = req.body.email;
const password = req.body.password;
console.log(`hello`)
auth.createUserWithEmailAndPassword(email, password).then(async userCredentials => {
const user = userCredentials.user
console.log(user.email)
await db.collection("users").add({
createdAt: Date.now(),
name: req.body.name,
email: email,
signupmethod: "email",
})
.then((docRef) => {
console.log("Document written with ID: ", docRef.id);
})
.catch((error) => {
return console.error("Error adding document: ", error);
});
res.redirect(`/dashboard`)
}).catch(error => {
res.render('signup', {error:error.message})
return console.log(error.message)
})
});
Res.render and res.redirect are not working for some reason.
Here is the post request code
const data = {
email: email,
password: password,
name:name,
}
fetch("/signup", {
method: "POST",
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(data)
}).then((response) => {
console.log(response);
})
When I submit the form, the post request sends, and the user is logged in, but nothing happens after that.
Why can't I redirect when the code is successful or render a page when there is an error? is there a problem with res?