kgnkrkmz
kgnkrkmz

Reputation: 57

Redirecting after fetch post in node.js with express

I have ejs page(/profile/addcalender) and I am taking data from that page by using fetch with post. I can take and process data in my serverside post method but ı can not redirect after it.

Here is the related part of the ejs page:

<li><button class="contact100"  id='nextstp' style="width: 100%; height: 100px; font-size: larger; padding-bottom: 0px;"> <span>SCHEDULE</span></button></li>

This is the related part of .js file connected to ejs to take data and where I use fetch:

function moveon(){  //Collecting data in here // Bu fonksiyon da dataları toplaman lazım !!
 var mailler = mails; // ALL mails
 var Title = document.getElementById("title").value; // Meeting Title
 var desc = document.getElementById("description").value; // Meeting Description

 if(!Title){alert("Cannot Submit without title"); return;}
 if(!desc){alert("Description is mandatory"); return;}
 if(mails.length < 1){alert("You need AT LEAST one attendee"); return;}

 var stuff = {title: Title, decription: desc, mails:mailler}
 console.log(stuff);
 console.log(JSON.stringify(stuff));

 var stuff2 = {title : "alex", description: "de souza"};
 options = {
   method : 'POST',
   redirect: 'follow',
   body: JSON.stringify(stuff),
   headers: {
     'Content-Type': 'application/json'
   }
 };

 fetch('/profile/addcalender', options);

}

Here is the where I take post request with express:

  router.get('/addcalender', (req,res) => {
  //console.log(req.user);

   res.render('calendermeet',{user:req.user});
  })

  router.post('/addcalender', userController.postCalenderMeet);

Here is where I process data in server-side in usercontroller:

module.exports.postCalenderMeet = (req, res, next) => {
console.log("POST CALENDAR MEEEET")

async function main() {

    var userCalendars = [];

    for (email of req.body.mails) {
        await User.findOne({
            email: email
        }).then(user => {
            if (user) {
                //console.log(user.name);
                //console.log(user.calender);
                userCalendars.push(user.calender)
            }
        }).catch(err => console.log(err));
    }

    userCalendars.push(req.user.calender);

    console.log("1");
    console.log(userCalendars);
    console.log("2");

    res.redirect("/profile/calender");

    return userCalendars;

  }

  main();



 };

In normally I take post requests from form and at the server-side redirecting works. But when I used fetch it does not redirecting and I am staying on the same page.

Upvotes: 2

Views: 2047

Answers (1)

Quentin
Quentin

Reputation: 943527

A redirect means "What you asked for can be found here".

It doesn't mean "Navigate the main browser window here".

The browser will follow the redirect automatically and make the response available via the fetch API.

If you want to navigate to a new page: don't use fetch. The entire point of it is to make an HTTP request without navigating.

Use a form instead.

Upvotes: 2

Related Questions