res.render throwing error Node.JS

我的梦境 提交于 2020-08-06 06:02:44

问题


I've been trying to make this blog, and when it comes to rendering a page it throws, "Res.Render is not a function..."

    let express = require("express"),
    app = express(),
    bodyParser = require("body-parser"),
    methodOV = require("method-override"),
    mongoose = require("mongoose"),
    passport = require("passport"),
    pL = require("passport-local"),
    pLM = require("passport-local-mongoose"),
    User = require("./user.js");

mongoose.connect("mongodb://localhost/max");

let maxBlog = new mongoose.Schema({
    title: String,
    author: String,
    content: String
});

let Blog = mongoose.model('Blog', maxBlog);

app.use(express.static("public"));

app.use(bodyParser.urlencoded({ extended: false }))

app.use(bodyParser.json());

app.use(methodOV("_method"));

app.set('view engine', 'ejs');

//AUTH

app.use(passport.initialize());
app.use(passport.session());

passport.use(new pL(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

app.post("/register", (req, res) =>{
    User.register(new User({username: req.body.username}), req.body.password, function(err, user){
        if(err){
            console.log(err);
            return res.render('register');
        }
        passport.authenticate("local")(req, res, function(){
           res.redirect("/secret");
        });
    });
});

// LOGIN ROUTES
app.get("/login", (req, res) =>{
   res.render("login"); 
});
//login logic
app.post("/login", passport.authenticate("local", {
    successRedirect: "/secret",
    failureRedirect: "/login"
}) ,(req, res) =>{
});

app.get("/logout", (req, res) => {
    req.logout();
    res.redirect("/");
});


const isLoggedIn = (req, res, next) =>{
    if(req.isAuthenticated()){
        return next();
    } else {
      res.redirect("/login");
    }
};

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

app.get("/", (req,res) => {
    res.redirect("/home");
});

app.get("/home", (req,res) => {
    res.render("index");
});

app.get("/blog", (req,res) => {
    Blog.find({},(err,res) => {
        if (err){
            res.send("An unexpected error occured. Please try again later.");
        } else {
            res.render("blog",{blogs:res});
        }
    });
});

app.post("/blog",isLoggedIn,(req,res) => {
    Blog.create({
        title: req.body.t,
        author: req.body.a,
        content: req.body.c
    }), (err,create) => {
        if (err){
            res.send("There was an error. Try again later.");
        } else {
            res.render("blogs");
        }
    };
});

app.get("/blog:id",(req,res) => {
    Blog.findById(req.params.id,(err,find) => {
        if (err){
            res.send("An error occured. Please try again later.");
        } else {
            res.render("show",{blog:find});
        }
    });
});

app.put("/blogs:id",isLoggedIn,(req,res) => {
    Blog.findByIdAndUpdate(req.params.id,({
        content: req.body.c,
        author: req.body.a
    }),(err,update) => {
        if (err){
            res.send("An error occured. Please try again later.");
        } else {
            res.redirect("/blogs"+req.params.id);
        }
    });
});

app.listen(8080);

Please help! Thank you, Ben This has been bothering for me for a lot of time. It is very weird and all solutions have not worked. I have tried a lot of things and nothing has worked, unfortunately. Thank you so much.


回答1:


This is due to an argument definition issue.

You can try changing the name of the first function argument to response:

app.get("/blog", (req,response) => {
    Blog.find({},(err,res) => {
        if (err){
            response.send("An unexpected error occured. Please try again later.");
        } else {
            response.render("blog",{blogs:res});
        }
    });
});



回答2:


I had the same error as I created a var named 'res' but 'res' was part of 'function(req, res)' which I already declared. I changed one of them to resp and the problem was solved.



来源:https://stackoverflow.com/questions/46626461/res-render-throwing-error-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!