Issue
Update: From some research, i cant render a value whose value is continuously changed by Observable in the subscribe() function. How do I then move the "onUserNameClick" function in subscribe and still have the functionality of redirecting t user profile only when I click on username
Am learning how to code by building a job board with a community, 3 months in. Am trying to access the postCreator id, and use it to redirect to user profile. What am I doing wrong? Can't console log it.
ngOnInit() {
this.postId = this.route.snapshot.paramMap.get('postId');
this.postsService.getPost(this.postId)
.subscribe(selectPostData => {
this.post = {
postId: selectPostData.data._id,
postTitle: selectPostData.data.postTitle,
postContent: selectPostData.data.postContent,
postVote: selectPostData.data.postVote,
postCreator: selectPostData.data.postCreator,
postCreatedAt: selectPostData.data.createdAt,
image: selectPostData.data.image
};
// console.log(this.post)
this.userId = this.post.postCreator._id
});
console.log(this.userId)
}
// getCreatorInfo(){
// this.post.postCreator
// console.log(this.post)
// }
onUserNameClick (userId: any){
this.router.navigate(['/user-profile', userId])
<a
href="/user-profile"
class="post-page__creator"
(click)="onUserNameClick(post.postCreator._id)"
>by
<span>
{{ post.postCreator.userName }}
</span>
</a>
Solution
you can try this way
<a href="javascript:void(0)" class="post-page__creator"
(click)="onUserNameClick(post.postCreator._id)">by
<span>
{{ post.postCreator.userName }}
</span>
</a>
Answered By - Siddhartha Mukherjee
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.