[英]Nextjs error - TypeError: Cannot read property 'useState' of null
[英]Cannot read property 'name' of null node
圖式
var campgroundSchema = new mongoose.Schema({
name: String,
image: String,
description: String,
_id: String
});
var Campground = mongoose.model("Campground", campgroundSchema);
app.get("/campgrounds/:id",function(req , res){
Campground.findById(req.params._id, function(err, foundCampgrounds){
if(err){
console.log(err);
}else{
res.render("show", {campground: foundCampgrounds});
}
});
});
顯示.ejs
<%- include('partials/header') %>
<h1>this is a show template</h1>
<p><%= campground.name %></p>
<img src="<%=campground.image%>">
<%- include('partials/footer') %>
錯誤
TypeError: /home/ec2-user/environment/Yelpcamp/v2/views/show.ejs:3`
1| <%- include('partials/header') %> `
2| <h1>this is a show template</h1>`
>> 3| <p><%= campground.name %></p>`
4| <img src="<%=campground.image%>">`
5|
6| <%- include('partials/footer') %>`
無法在 show (/home/ec2-user/environment/Yelpcamp/v2/) 上讀取 null 的屬性“名稱” node_modules/ejs/lib/ejs.js:656:17) 在 tryHandleCache (/home/ec2-user/environment/Yelpcamp/v2/node_modules/ejs/lib/ejs.js:254:36) 在 View.exports.renderFile [作為引擎] (/home/ec2-user/environment/Yelpcamp/v2/node_modules/ejs/lib/ejs.js:459:10) 在 View.render (/home/ec2-user/environment/Yelpcamp/v2/ node_modules/express/lib/view.js:135:8) 在 tryRender (/home/ec2-user/environment/Yelpcamp/v2/node_modules/express/lib/application.js:640:10) 在 Function.render (/ home/ec2-user/environment/Yelpcamp/v2/node_modules/express/lib/application.js:592:3) 在 ServerResponse.render (/home/ec2-user/environment/Yelpcamp/v2/node_modules/express/lib/ response.js:1012:7) 在/home/ec2-user/environment/Yelpcamp/v2/app.js:74:17 在/home/ec2-user/environment/Yelpcamp/v2/node_modules/mongoose/lib/model .js:4828:16 在 /home/ec2-user/environment/Yelpc amp/v2/node_modules/mongoose/lib/query.js:4390:12 在 model.Query.Query._completeOne (/home/ec2-user/environment/Yelpcamp/v2/node_modules/mongoose/lib/query.js:2074 :12) 在 Immediate.Query.base.findOne.call (/home/ec2-user/environment/Yelpcamp/v2/node_modules/mongoose/lib/query.js:2136:10) 在 Immediate。 (/home/ec2-user/environment/Yelpcamp/v2/node_modules/mquery/lib/utils.js:116:16) 在 runCallback (timers.js:705:18) 在 tryOnImmediate (timers.js:676:5)
即使在重新啟動 mongodb 后我仍收到此錯誤,我如何檢查名稱是否為 null
布魯它必須是沒有下划線的 req.params.id:)
解決辦法是:
我也遇到了同樣的問題,但是當我從 /campgrounds 頁面打開時它就解決了
問題的解決有點簡單,就在這行代碼中
res.redirect("/campgrounds"{campground:newCampgrounds})
您可能缺少將newCampgrounds
引用到將在 show.ejs 中用作 campground.name 或 campground.image 等的campground.name
campground.image
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.