Sadeghbayan
Sadeghbayan

Reputation: 1163

Redirect user after submit in node js

I'm new to node and mongodb , i want to display data after submit form but i got blang page .
app.js

app.post("/new", function(req, res){
    new user({
        _id : req.body.email,
        name: req.body.name,
        age : req.body.age
    }).save(function(err, doc){
            if(err){
                res.json(err);
            }else{
                res.redirect("/view");
            }
        });
    console.log(res);

});

app.get("/view", function(req, res){
    user.find({}, function(err, doc){
        if(err)  res.json(err)
        else     res.render("view", {users:doc});
    });
});  

in view.jade

extends layout
ul
each user in users
    li #{user.name}  

in view.jade i got nothing, even html tag in body .

Upvotes: 1

Views: 2143

Answers (1)

tuvokki
tuvokki

Reputation: 740

I assume you are using express, and this is possibly what you are looking for:

For example, from http://example.com/blog/admin/ (notice the trailing slash), the following would redirect to the URL http://example.com/blog/admin/post/new.

res.redirect('/view');

In your example this would be:

app.post("/new", function(req, res){
    new user({
        _id : req.body.email,
        name: req.body.name,
        age : req.body.age
    }).save(function(err, doc){
            if(err){
                res.json(err);
            }else{
                res.redirect("/view");
            }
        });
    console.log(res);
    res.redirect('/view'); //here the redirect takes place
});

Upvotes: 1

Related Questions