javascript - 类型错误 : Cannot read property 'name' of null

标签 javascript node.js mongodb mongoose mongoose-schema

<分区>

我的 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)

最佳答案

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

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

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

关于javascript - 类型错误 : Cannot read property 'name' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63420697/

相关文章:

mongodb - 导出 Mongodb 图表

javascript - Javascript 中的 RegEx 允许负十进制输入到文本字段

javascript - Quasar Cordova Google map 插件

javascript - Grunt 匹配正则表达式

javascript - Socket.IO - 如何向房间里的每个人发出事件,包括发件人?

mongodb - 如何将 Geojson 文件导入 MongoDB

c# - MongoDB C# 驱动程序检查身份验证状态和角色

javascript - 我如何一次显示/隐藏一个 ng-repeat 元素?

javascript - Angularjs - ng-click 函数与指令

node.js - 等待nodejs异步任务完成