所以当我的用户发表评论时,我想把他们的头像URL存储在评论对象上,这样我就可以很容易地访问它,但我认为这是行不通的,因为如果他们改变了头像或删除了它,评论仍然会包含他们的旧URL,我也尝试过在Firestore中存储对用户的引用,但我不确定是我做错了还是什么,因为我一直遇到错误。
TLDR -我想问的是,是否有人知道如何存储和访问特定评论的URL (将来可能会发生变化)。
抱歉,如果我没有澄清或解释事情,我是一个很新的反应,你可能已经知道了。如果任何人有任何问题,我可以尝试更好地解释,所以是的,谢谢你阅读这篇文章,并提前感谢。
import React, { useEffect, useState } from 'react';
import { postComment, deleteComment } from '../../store/actions/commentsActions';
import { connect, useSelector } from 'react-redux';
import { useFirestore } from 'react-redux-firebase';
import { useForm } from 'react-hook-form';
import { toast } from 'react-toastify';
import moment from 'moment';
import { ErrorCircle } from '@styled-icons/boxicons-solid/ErrorCircle';
import { Error } from '@styled-icons/boxicons-solid/Error';
import { Modal } from '../../helpers/Modal';
import ProfilePlaceHolder from '../../assets/images/user.svg';
import Loading from '../../helpers/Loading';
export const Comments = (props) => {
const { auth, match, history, commentError } = props;
const slug = match.params.slug;
const firestore = useFirestore();
const profile = useSelector(state => state.firebase.profile);
const { register, handleSubmit, reset } = useForm();
const [comments, setComments] = useState([]);
const [loading, setLoading] = useState(true);
useEffect(() => {
const listener =
firestore
.collection('posts')
.doc(slug)
.collection('comments')
.orderBy('createdAt', 'desc')
.onSnapshot((snapshot) => {
let _comments = [];
snapshot.forEach(commentSnapshot => {
const thisComment = commentSnapshot.data();
_comments.push({commentData: thisComment, commentId: commentSnapshot.id});
});
setComments(_comments);
setLoading(false);
}, (error) => {
console.log(error);
});
return () => listener();
}, [firestore, slug]);
const postComment = async (formData) => {
if (auth.isEmpty) {
toast.error('You are not authenticated ?');
return;
}
await props.postComment({formData, slug});
reset();
};
const deleteComment = (commentId, authorId) => {
const currentUserId = auth.uid;
const commentUserId = authorId;
if (!comments) {
return;
}
if (currentUserId !== commentUserId) {
toast.error('That\'s not your comment')
return;
}
props.deleteComment({commentId, authorId, slug});
};
const back = () => {
history.goBack();
};
if (loading) {
return <Loading />;
};
return (
<div className='main' style={{ width: '600px', maxWidth: '90%' }}>
{
commentError !== null ? (
<span className='error-message'>
<ErrorCircle size='30' style={{ marginRight: 5 }} />
{commentError}
</span> ) : null
}
<div className='long-container' onClick={back} style={{ cursor: 'pointer', height: '50px' }}>
Commenting on the post: {slug}
</div>
<div className='long-container' style={{ padding: '10px 0' }}>
<div>
<img
src={profile.profilePictureURL ?? ProfilePlaceHolder}
alt='Profile'
className='profile-picture'
/>
<span className='usertag-span'>{auth?.displayName}</span>
</div>
<div>
<form onSubmit={handleSubmit(postComment)}>
<textarea
name='content'
rows='3'
disabled={!auth}
style={{ margin: '10px 0' }}
placeholder='Add to the conversation!'
ref={register({ required: true })}
/>
<span style={{ width: '90%' }}>
<button>Comment</button>
</span>
</form>
</div>
</div>
{comments.map((comment) =>
<div key={comment.commentId} className='long-container' style={{ padding: '15px 0' }}>
<div style={{ height: '30px' }}>
<img
src={comment.commentData.authorProfilePicture ?? ProfilePlaceHolder}
alt='Profile'
className='profile-picture'
/>
<div className='commentMetadata' style={{ flexDirection: 'column', alignItems: 'flex-start', justifyItems: 'center' }}>
<span className='usertag-span'>{comment.commentData.author}</span>
<span>{moment(comment.commentData.createdAt?.toDate()).fromNow()}</span>
</div>
</div>
<span className='commentText-span'>
{comment.commentData.content}
</span>
<span className='commentText-span' style={{ justifyContent: 'flex-end' }}>
{
auth.uid === comment.commentData.authorId ?
(
<Modal
buttonActionClassName='delete-button'
visibleButtonClassName='delete-button'
modalContentHeaderBackgroundColor='#fa4949'
title='Confirm'
modalContent='Are you sure you want to delete this comment?'
emoji={<Error size='30' color='#f53d3d' style={{ marginRight: 10 }} />}
buttonActionName='Delete'
buttonAction={() => deleteComment(comment.commentId, comment.commentData.authorId)}
/>
) : null
}
</span>
</div>
)}
</div>
)
}
const mapDispatchToProps = (dispatch) => {
return {
postComment: (comment) => dispatch(postComment(comment)),
deleteComment: (commentToDelete) => dispatch(deleteComment(commentToDelete))
}
}
const mapStateToProps = (state) => {
return {
auth: state.firebase.auth,
commentError: state.commentsReducer.commentError,
}
}
export default connect(mapStateToProps, mapDispatchToProps)(Comments);这也是我尝试过的东西,我提到过,但并不完全有效,哈
useEffect(() => {
const listener = firestore
.collection('posts')
.doc(slug)
.collection('comments')
.onSnapshot((snapshot) => {
let _comments = [];
snapshot.forEach(commentSnapshot => {
_comments.push(commentSnapshot.data());
setComments(_comments);
});
_comments.map((comment) => {
return comment.authorRef.get().then(snapshot => {
const { name, profilePictureURL } = snapshot.data();
setAuthorInfo({ name, profilePictureURL });
if (snapshot.data()) {
console.log(authorInfo.profilePictureURL)
}
}) })
}, (error) => {
console.log(error);
});
return () => listener();
}, [firestore, slug, comments, authorInfo]);发布于 2020-04-03 08:51:01
我不是React方面的专家,但也许使用DocumentReference会对引用存储在Firestore中的URL有所帮助。Here你可以找到让它工作的文档。我假设URL与用户ID链接在一起,因此您也可以使用它来获取图像URL。
发布于 2020-04-03 10:02:44
我不确定您是否可以获取不是当前登录用户的Firebase用户的信息。
我的建议是将每个用户的信息存储在数据库中,然后每次用户登录时,您都可以在必要时更新数据库。然后,当显示用户的评论时,您可以在数据库中查找该用户的图片。
发布于 2021-01-21 04:05:29
只需在需要渲染用户个人资料图片的地方使用它
<img src={auth.photo} />并确保在App.js中的应用程序()中有这个
useEffect(() => {
auth.onAuthStateChanged((authUser) => {
console.log("user is ", authUser);
if(authUser){
dispatch(login({
uid: authUser.uid,
photo: authUser.photoURL,
email: authUser.email,
displayName: authUser.displayName,
}))
}
else{
dispatch(logout());
}
})
}, [dispatch])https://stackoverflow.com/questions/61001666
复制相似问题