console.log(user);
it is re me an Object in which the displayName
has a Value that is a string. 但是,当我做<条码>console.log(用户:displayName);时,它应当给我。 Agrim Singh (<- I ve 此数值)。 但是,它提出了一个错误:
差价:没有读出无效的财产(仿照Name)
Here is an image of the error:
<>Here is the Code>[postid].jsx:
const post = ({ post, user }) => {
const [name, setName] = useState("");
const [addComment, setAddComment] = useState("");
const router = useRouter();
const { postid } = router.query;
console.log(user); // <--- Working perfectly.
console.log(user.displayName); // <--- Throwing error.
const handleSubmit = (e) => {
e.preventDefault();
if (!addComment || !name) {
alert("Please fill all the fields proplerly!");
} else {
db.collection("posts").doc(postid).collection("comments").add({
name: name, // <--- Please ignore this line for now
comment: addComment,
});
}
};
return (
<div className="post container text-center">
<h2>{post.title}</h2>
<h5>Created On - {new Date(post.createdAt).toDateString()}</h5>
<br />
<img src={post.imageUrl} alt={post.alt} />
<br />
<p>{post.description}</p>
<br />
<form>
<div className="form-input">
<input
type="text"
name="name"
id="name"
className="name"
value={name}
onChange={(e) => setName(e.target.value)}
placeholder="Enter Your Name"
/>
</div>
<div className="form-input">
<textarea
name="add-comment"
id="add-comment"
className="add-comment"
value={addComment}
onChange={(e) => setAddComment(e.target.value)}
cols="100"
rows="3"
placeholder="Add your Comment"
></textarea>
</div>
<button onClick={handleSubmit} className="btn-warning">
Submit
</button>
</form>
<div className="comments">
<p className="comment">
<b>
<i>Name</i>:
</b>{" "}
<span>Comment</span> {/* <--- I ve not programmed this line because of the error. */}
</p>
</div>
</div>
);
};
export default post;
export async function getServerSideProps({ params }) {
const result = await db.collection("posts").doc(params.postid).get();
return {
props: {
post: {
...result.data(),
createdAt: result.data().createdAt.toMillis(),
},
},
};
}
页: 1 文档中的所有档案均以<代码>_app.js 。
该守则是:
export default function App({ Component, pageProps }) {
const [user, setUser] = useState(null);
useEffect(() => {
auth.onAuthStateChanged((user) => {
if (user) {
setUser(user);
} else {
setUser(null);
}
});
}, []);
return (
<div className="app">
<div className="banner-div">
<Navbar user={user} />
<img src="/banner.jpg" alt="School Banner" className="banner" />
</div>
<Component {...pageProps} user={user} />
</div>
);
}