簡體   English   中英

無法讀取 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:)

解決辦法是:

  1. 重啟服務器后,打開“/campgrounds”頁面,在我的例子中是“http://localhost:3000/campgrounds”
  2. 然后點擊更多信息。

我也遇到了同樣的問題,但是當我從 /campgrounds 頁面打開時它就解決了

問題的解決有點簡單,就在這行代碼中

res.redirect("/campgrounds"{campground:newCampgrounds})

您可能缺少將newCampgrounds引用到將在 show.ejs 中用作 campground.name 或 campground.image 等的campground.name campground.image

firebase:無法分配給 object 的只讀屬性“displayName”'#<object> '<div id="text_translate"><p> 我在我的 ReactJS 項目上使用@reduxjs/toolkit: ^1.7.1 、 firebase: 9.6.1和 firebase firebase-tools: 10.0.1 。 我試圖創建一個 function 用戶可以在其中更新他們的姓名和頭像照片。</p><p> 為此,我使用了updateProfile() function。 但是,每當我執行更新 function 時,它都會引發錯誤<strong>Cannot assign to read only property 'displayName' of object '#&lt;Object&gt;'</strong> 。</p><p> 我注意到一件有趣的事情是,如果我只更新 photoURL 屬性,它仍然Cannot assign to read only property 'displayName' of object '#&lt;Object&gt;'</p><p> <a href="https://github.com/mdibuhossain/Front-end-ReactJS-with-Firebase-template" rel="nofollow noreferrer">Project_Link_Github</a></p><p> 代碼: useFirebase.js : </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> import { getAuth, signInWithPopup, GoogleAuthProvider, createUserWithEmailAndPassword, signInWithEmailAndPassword, signOut, onAuthStateChanged, updateProfile } from 'firebase/auth'; import { getDownloadURL, getStorage, ref, uploadBytes } from 'firebase/storage'; import { useEffect, useState } from 'react'; import { useDispatch } from 'react-redux'; import { useLocation, useNavigate } from 'react-router-dom'; import { setIsLoading } from '../features/isloadingSlice'; import { login, logout } from '../features/userSlice'; import initAuth from '../Firebase/initAuth'; initAuth(); export const useFirebase = () =&gt; { const auth = getAuth(); const location = useLocation(); const navigate = useNavigate(); const dispatch = useDispatch(); const [updateCount, setUpdateCount] = useState(0); const storage = getStorage(); const Redirect = () =&gt; { console.log(location); const destination = location?.state?.from?.pathname || '/'; navigate(destination); } const uploadAvatar = async (file) =&gt; { const fileRef = ref(storage, 'avatar/' + auth?.currentUser?.uid + '.png'); dispatch(setIsLoading(true)); const snapshot = await uploadBytes(fileRef, file); const photoURL = await getDownloadURL(fileRef); updateProfile(auth.currentUser, { photoURL }).then(() =&gt; { setUpdateCount(updateCount + 1); }).catch(e =&gt; console.log(e.message)) dispatch(setIsLoading(false)); console.log(snapshot); } const userRegister = (name, photoURL, email, password) =&gt; { dispatch(setIsLoading(true)); createUserWithEmailAndPassword(auth, email, password).then(result =&gt; { updateProfile(auth.currentUser, { displayName: name, photoURL }).then(() =&gt; { }) dispatch(login({ displayName: name, email, photoURL })); Redirect(); }).catch(error =&gt; alert(error.message)).finally(() =&gt; dispatch(setIsLoading(false))) } useEffect(() =&gt; { const unsubscribe = onAuthStateChanged(auth, (result) =&gt; { if (result) { dispatch(login({...result })) } else { dispatch(login({})) } dispatch(setIsLoading(false)); }) return () =&gt; unsubscribe; }, [updateCount, auth]) return { logIn, logOut, Redirect, uploadAvatar, userRegister, } }</pre></div></div><p></p><p> <a href="https://i.stack.imgur.com/6CJQm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6CJQm.png" alt="在此處輸入圖像描述"></a></p><p> 我不知道這個<strong>displayName</strong>屬性有什么問題,但我以前的項目工作正常。</p><p> <strong>有人可以幫我嗎?</strong></p></div></object>

[英]firebase: Cannot assign to read only property 'displayName' of object '#<Object>'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 Nextjs 錯誤 - 類型錯誤:無法讀取 null 的屬性“useState” cannot read property handleSetJSResponder of null error in react native v0.70.3 TypeError:無法讀取 null 的屬性(讀取“uid”) 無法讀取 null 的屬性(讀取“電子郵件”) '無法讀取 nuxtJs/firebase 中未定義的屬性'應用程序' 未捕獲的類型錯誤:無法讀取未定義的屬性“initializeApp” Cloud Spanner 錯誤:無法讀取 null 的屬性(讀取“加入”) Flutter web 和 Firebase 身份驗證類型錯誤:無法讀取未定義的屬性“應用程序” Knex:無法使用異步連接讀取未定義的屬性“客戶端” firebase:無法分配給 object 的只讀屬性“displayName”'#<object> '<div id="text_translate"><p> 我在我的 ReactJS 項目上使用@reduxjs/toolkit: ^1.7.1 、 firebase: 9.6.1和 firebase firebase-tools: 10.0.1 。 我試圖創建一個 function 用戶可以在其中更新他們的姓名和頭像照片。</p><p> 為此,我使用了updateProfile() function。 但是,每當我執行更新 function 時,它都會引發錯誤<strong>Cannot assign to read only property 'displayName' of object '#&lt;Object&gt;'</strong> 。</p><p> 我注意到一件有趣的事情是,如果我只更新 photoURL 屬性,它仍然Cannot assign to read only property 'displayName' of object '#&lt;Object&gt;'</p><p> <a href="https://github.com/mdibuhossain/Front-end-ReactJS-with-Firebase-template" rel="nofollow noreferrer">Project_Link_Github</a></p><p> 代碼: useFirebase.js : </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> import { getAuth, signInWithPopup, GoogleAuthProvider, createUserWithEmailAndPassword, signInWithEmailAndPassword, signOut, onAuthStateChanged, updateProfile } from 'firebase/auth'; import { getDownloadURL, getStorage, ref, uploadBytes } from 'firebase/storage'; import { useEffect, useState } from 'react'; import { useDispatch } from 'react-redux'; import { useLocation, useNavigate } from 'react-router-dom'; import { setIsLoading } from '../features/isloadingSlice'; import { login, logout } from '../features/userSlice'; import initAuth from '../Firebase/initAuth'; initAuth(); export const useFirebase = () =&gt; { const auth = getAuth(); const location = useLocation(); const navigate = useNavigate(); const dispatch = useDispatch(); const [updateCount, setUpdateCount] = useState(0); const storage = getStorage(); const Redirect = () =&gt; { console.log(location); const destination = location?.state?.from?.pathname || '/'; navigate(destination); } const uploadAvatar = async (file) =&gt; { const fileRef = ref(storage, 'avatar/' + auth?.currentUser?.uid + '.png'); dispatch(setIsLoading(true)); const snapshot = await uploadBytes(fileRef, file); const photoURL = await getDownloadURL(fileRef); updateProfile(auth.currentUser, { photoURL }).then(() =&gt; { setUpdateCount(updateCount + 1); }).catch(e =&gt; console.log(e.message)) dispatch(setIsLoading(false)); console.log(snapshot); } const userRegister = (name, photoURL, email, password) =&gt; { dispatch(setIsLoading(true)); createUserWithEmailAndPassword(auth, email, password).then(result =&gt; { updateProfile(auth.currentUser, { displayName: name, photoURL }).then(() =&gt; { }) dispatch(login({ displayName: name, email, photoURL })); Redirect(); }).catch(error =&gt; alert(error.message)).finally(() =&gt; dispatch(setIsLoading(false))) } useEffect(() =&gt; { const unsubscribe = onAuthStateChanged(auth, (result) =&gt; { if (result) { dispatch(login({...result })) } else { dispatch(login({})) } dispatch(setIsLoading(false)); }) return () =&gt; unsubscribe; }, [updateCount, auth]) return { logIn, logOut, Redirect, uploadAvatar, userRegister, } }</pre></div></div><p></p><p> <a href="https://i.stack.imgur.com/6CJQm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6CJQm.png" alt="在此處輸入圖像描述"></a></p><p> 我不知道這個<strong>displayName</strong>屬性有什么問題,但我以前的項目工作正常。</p><p> <strong>有人可以幫我嗎?</strong></p></div></object>
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM