const [userObj , setUserObj] = useState("");
const [ReplyObj , setReply] = useState("");
No value is associated with the useState listed above. I tried the following to assign a value with useState but it doesn’t work.
setReply(response);
export default function Forumview() {
let userData =""
const [userObj , setUserObj] = useState("");
const [ReplyObj , setReply] = useState("");
const [forum , setForum] = useState("");
const {id} = useParams()
useEffect(() => {
Getforum(id);
getUser();
getReplies(id);
},[id]);
const getUser= () =>{
userData = localStorage.getItem("user");
setUserObj(JSON.parse(userData))
console.log(userObj.fName +" "+ userObj.lName)
}
const Getforum = (id) =>{
console.log("Forum ID " + id);
fetch("/forumget/one/" + id).then(res=>res.json())
.then(response=>{
setForum(response);
console.log("GetForum Response " + response);
console.log("Forum " + forum);
})
.catch((err)=>{
console.log("Err - ",err)
})
}
// Reply Get
const getReplies = (id) =>{
fetch("/reply/single/" + id).then(res=>res.json())
.then(response=>{
setReply(response);
console.log("Get Replies Response " + response);
console.log("ReplyObj " + ReplyObj);
})
.catch((err)=>{
console.log("Err - ",err)
})
}
return (
<div>
<Header/>
<div className='forumview-view'>
<div className='forumview-topic'>
<h1 >{forum.Title}</h1>
<p className='forumview-topic-txt'>{forum.Created_at}</p>
</div>
<hr/>
<div className='forumview-topic-desc'>
<div>{forum.Body}</div>
<img src={forum.Pic} />
</div>
</div>
<div className='forumview-reply'>
<div className='forumview-btn-container'>
<div className='forumview-topic-txt'>
<h6 className='forumview-topic-txt-reply'>Replies</h6>
</div>
</div>
<br/>
<hr/>
<div className='forumview-reply-card'>
</div>
<ReplyHome getForumid={id} GetUser={userObj}/ >
<div>
{/* {
// forum.map(getReplies => { */}
<div className='replycard-mainbox'>
<div className='replycard-container'>
<p><b>nnn</b></p>
<p>This was a good start. These files have a name sapace associated with them,which does throw in a curve ball. <br/>
The easiest way to handle the namespaces is to strip them out.Also,ensure the correct file is referenced in the xml_find() functions.</p>
<i class="fa-solid fa-heart"></i><i class="fa-solid fa-trash-can"></i><a href="/replyupdate"><i class="fa-solid fa-pen-to-square"></i></a>
</div>
</div>
{/* })} */}
</div>
</div>
<Footer/>
</div>
)
}
Backend
//Get Specific Reply
router.get('/reply/single/:id', async (req,res)=>{
try{
const fid = req.params.id;
console.log(fid)
Reply.find({forum_id:fid})
.populate("user","_id fName lName")
.then((ReplyData)=>{
res.status(200).json(ReplyData)
}).catch((err)=>{
console.log(err);
return res.status(400).json({ error: "Something has error" });
})
}catch{
return res.status(400).json({ error: "Something has error" });
}
});
Console OUTPUT
Postmen Output ( Get Specific Reply)