我一直試圖讓這個博客,當涉及到渲染它拋出一個頁面,「Res.Render不是一個函數...」res.render拋出錯誤的node.js
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);
請幫忙!謝謝,本很長時間以來一直困擾着我。這很奇怪,所有的解決方案都沒有奏效。不幸的是,我嘗試了很多東西,沒有任何工作。非常感謝。
其功能是一個投擲的錯誤? –
以下是錯誤:TypeError:res.render不是在查詢中的Blog.find(/Users/ben/Desktop/ShoeAdict/app.js:92:8) 處的函數 。(/Users/ben/Desktop/ShoeAdict/node_modules/mongoose/lib/model.js:3911:16) at /Users/ben/Desktop/ShoeAdict/node_modules/kareem/index.js:273:21 at/users/ben/Desktop/ShoeAdict/node_modules/kareem/index.js:131:16 at _combinedTickCallback(internal/process/next_tick.js:73:7) at process._tickCallback(internal/process/next_tick.js:104 :9) –
Blaksky
聽起來像一個範圍問題。你正嘗試在另一個函數中調用res.render,該函數也有一個沒有渲染函數的res參數 –