类型错误:无法读取null的属性“名称”

问题描述

我的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",res){
    Campground.find({},function(err,allcampgrounds){
        if(err){
            console.log()
        } else {
            res.render("index",{campgrounds:allcampgrounds});
        }
    })
    
})

app.post("/campgrounds",res){
    var name = req.body.name;
    var image = req.body.image;
    var newCampground = {name : name,image: image}
    Campground.create(newCampground,newlyCreated){
        if(err){
            console.log(err)
        } else {
            res.redirect("/campgrounds")
        }
    })
    
})

app.get("/campgrounds/new",res){
    res.render("new.ejs")
})

app.get("/campgrounds/:id",res){
    Campground.findById(req.params._id,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 |

无法读取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)

解决方法

更改 Campground.findById(req.params._id,function(err,foundCampground)

Campground.findById(req.params.id,foundCampground)

因为您需要提供来自“ / campgrounds /:id”路线的ID