<分区>
我的 nodejs 服务器连接到 mongoDB 时遇到问题
这是app.js的主要代码
var express = require("express")
var app = express();
var bodyParser = require("body-parser");
var mongoose = require("mongoose")
mongoose.connect("mongodb://localhost:27017/yelpcamp", {useNewUrlParser: true, useUnifiedTopology: true});
app.use(bodyParser.urlencoded({extended: true}));
app.set("view engine", "ejs");
var campgroundSchema = new mongoose.Schema({
name: String,
image: String,
description: String
});
var Campground = mongoose.model("Campground", campgroundSchema);
app.get("/", function(req,res){
res.render("landing")
})
app.get("/campgrounds", function(req,res){
Campground.find({}, function(err, allcampgrounds){
if(err){
console.log()
} else {
res.render("index", {campgrounds:allcampgrounds});
}
})
})
app.post("/campgrounds", function(req,res){
var name = req.body.name;
var image = req.body.image;
var newCampground = {name : name, image: image}
Campground.create(newCampground, function(err, newlyCreated){
if(err){
console.log(err)
} else {
res.redirect("/campgrounds")
}
})
})
app.get("/campgrounds/new", function(req,res){
res.render("new.ejs")
})
app.get("/campgrounds/:id", function(req,res){
Campground.findById(req.params._id, function(err, foundCampground){
if(err){
console.log(err)
} else {
console.log(foundCampground)
res.render("show", {campground: foundCampground})
}
})
})
app.listen(3000, function(){
console.log("The Server has started listening on Port 3000!")
})
那是我的主要路线,我遇到的问题是在显示页面上使用/campgrounds/:id 的最后一条路线,我只是想打电话
它只是返回这个错误:
TypeError: /mnt/c/Users/zhelf/Downloads/test/yelpcamp/views/show.ejs:4 2| 3|
4|
<%= campground.name %>
无法读取 null 的属性“名称” 在评估 (/mnt/c/Users/zhelf/Downloads/test/yelpcamp/views/show.ejs:10:37) 在展会上 (/mnt/c/Users/zhelf/Downloads/test/yelpcamp/node_modules/ejs/lib/ejs.js:691:17) 在 tryHandleCache (/mnt/c/Users/zhelf/Downloads/test/yelpcamp/node_modules/ejs/lib/ejs.js:272:36) 在 View.exports.renderFile [作为引擎] (/mnt/c/Users/zhelf/Downloads/test/yelpcamp/node_modules/ejs/lib/ejs.js:489:10) 在 View.render (/mnt/c/Users/zhelf/Downloads/test/yelpcamp/node_modules/express/lib/view.js:135:8) 在 tryRender (/mnt/c/Users/zhelf/Downloads/test/yelpcamp/node_modules/express/lib/application.js:640:10) 在 Function.render (/mnt/c/Users/zhelf/Downloads/test/yelpcamp/node_modules/express/lib/application.js:592:3) 在 ServerResponse.render (/mnt/c/Users/zhelf/Downloads/test/yelpcamp/node_modules/express/lib/response.js:1012:7) 在/mnt/c/Users/zhelf/Downloads/test/yelpcamp/app.js:85:17 在/mnt/c/Users/zhelf/Downloads/test/yelpcamp/node_modules/mongoose/lib/model.js:4824:16 在/mnt/c/Users/zhelf/Downloads/test/yelpcamp/node_modules/mongoose/lib/model.js:4824:16 在/mnt/c/Users/zhelf/Downloads/test/yelpcamp/node_modules/mongoose/lib/helpers/promiseOrCallback.js:24:16 在/mnt/c/Users/zhelf/Downloads/test/yelpcamp/node_modules/mongoose/lib/model.js:4847:21 在/mnt/c/Users/zhelf/Downloads/test/yelpcamp/node_modules/mongoose/lib/query.js:4390:11 在/mnt/c/Users/zhelf/Downloads/test/yelpcamp/node_modules/kareem/index.js:135:16 在 processTicksAndRejections (internal/process/task_queues.js:79:11)