简体   繁体   中英

Redirect to new url after form POST mongo save()

This code is incorrect. Redirect only redirects, I don't think you can pass results through it.

router.post('/form', function(req, res, next){
  var student = new StudentModel({
    firstName: req.body.firstName,
    lastName: req.body.lastName,
  });

  student.save(function (err) {
    if (err) return handleError(err);
    db.collection("studentmodels").find().toArray(function(err, result) {
      if (err) throw err;
        res.redirect('/submitted', { data: result});  -- what can I do here?
    });
  });
});

Is there a way to redirect and include this information?
I would like to use:

res.render('submitted', { data: result})

But I can't change the URL path

There are some ways to pass the data to the new route.

you can render the submitted template file by passing data if you don't want to change the URI

If you want the specific URI you can pass the data as query params into your route you can see the example in the below https://stackoverflow.com/a/19038048/6551916

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM