
* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.
* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.
firebase에서 댓글을 가져오는 방법입니다. 현재 오름차순인데 내림차순으로 가져 올수 없나요.
아래 코드는 orderBy를 했는데 안먹히고 있어요 다른 방법좀 부탁 합니다.
sort방법도 있나 본데... 제가 하니 잘 안돼요.적용도 잘 안됀듯 하고..이래저래 모르겠어요. ㅠㅠ
게시글을 댓글은 comment에 넣어 있구요.게시글은 did라는키값으로 댓글을 가져오는건데 저장된 순서로 가져오는데요 이것을 내림차순으로 가져오고 싶어요.
------------- firebase코드
export async function getComment(did) {
const db = firebase.firestore();
let data = [];
const q = await db.collection('comment').where('did', '==', did).get();
console.log(q);
if (q.empty) {
console.log('No matching documents.');
return 0;
} else {
q.forEach((doc) => {
console.log(doc.id, '=>', doc.data());
data.push(doc.data());
});
return data;
}
}
-------------------------------디테일페이지코드--------------------------------------------------
export default function DetailPage({ navigation, route }) {
const [commentInput, setCommentInput] = useState('');
const [comment, setComment] = useState([]);
const [commentspace, setCommentspace] = useState(false);
const content = route.params.content;
useEffect(() => {
navigation.setOptions({
title: '디테일페이지',
headerStyle: {
backgroundColor: '#fff',
shadowColor: '#fff',
},
headerTintColor: 'grey',
headerShown: true,
headerBackTitleVisible: false,
});
commentLoad(content.date);
}, []);
// 댓글을 가져오는 부분
const commentLoad = async (did) => {
console.log('firebase에서 댓글 가져오기');
console.log(did);
let c = await getComment(did + 'D');
if (c == 0) {
} else {
setComment(c);
}
};
const commentFunc = async () => {
let date = new Date();
let getTime = date.getTime();
const currentUser = firebase.auth().currentUser;
let newComment = {
date: getTime,
comment: commentInput,
did: content.date + 'D',
uid: currentUser.uid,
};
let result = await addComment(newComment);
if (result) {
Alert.alert('댓글이 정상적으로 저장되었습니다!');
setCommentInput('');
await setComment([...comment, newComment]);
}
};
return (
<Container>
<Content
contentContainerStyle={{
alignItems: 'center',
marginTop: 20,
}}
>
<ImageBlurLoading
withIndicator
thumbnailSource={{ uri: content.image }}
source={{ uri: content.image }}
style={{ width: '90%', height: 200, borderRadius: 10 }}
/>
<Text
style={{
fontSize: 25,
fontWeight: '700',
color: '#333',
alignSelf: 'flex-start',
marginLeft: 25,
marginTop: 20,
}}
>
{content.title}
</Text>
<Text
style={{
fontSize: 15,
fontWeight: '700',
color: 'grey',
alignSelf: 'flex-start',
marginLeft: 25,
marginTop: 20,
}}
>
{content.desc}
</Text>
<Item style={style.input_color}>
<Input
placeholder="한마디 부탁해요~"
value={commentInput}
onChangeText={(text) => {
setCommentInput(text);
}}
/>
<Icon
active
name="paper-plane"
onPress={() => {
commentFunc();
}}
/>
</Item>
<List>
{comment.map((c, i) => {
return <CommentComponet key={i} comment={c} />;
})}
</List>
</Content>
</Container>
);
}
const styles = StyleSheet.create({
input_color: {
marginTop: 100,
color: 'blue',
},
});
