사용자를 찾을 수 없다.
378
작성자 없음
投稿した質問数 0
안녕하세요, 강의를 따라하던 중
사용자를 찾을 수 없다는 페이지가 떠서 어디서 오류가 났는지 확인을 못하겠어서 질문드립니다.
혹시몰라 이 강의 까지 커밋한 깃허브 주소 올려봅니다,,
https://github.com/cccodus313/Blah-Blah/tree/start
import { ServiceLayout } from '@/components/service_layout';
import { useAuth } from '@/contexts/auth_user.context';
import { InAuthUser } from '@/models/in_auth_user';
import {
Avatar,
Box,
Button,
Flex,
FormControl,
FormLabel,
Switch,
Text,
Textarea,
useToast,
VStack,
} from '@chakra-ui/react';
import { GetServerSideProps, NextPage } from 'next';
import { useState, useEffect } from 'react';
import ResizeTextarea from 'react-textarea-autosize';
import axios, { AxiosResponse } from 'axios';
import MessageItem from '@/components/message_item';
import { InMessage } from '@/models/message/in_message';
interface Props {
userInfo: InAuthUser | null;
}
async function postMessage({
message,
uid,
author,
}: {
message: string;
uid: string;
author?: {
displayName: string;
photoURL?: string;
};
}) {
if (message.length <= 0) {
return {
result: false,
message: '내용을 입력해주세요',
};
}
try {
await fetch(`/api/message.add`, {
method: 'post',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({
uid,
message,
author,
}),
});
return {
result: true,
};
} catch (err) {
console.error(err);
return {
result: false,
message: '등록 실패',
};
}
}
const UserHomePage: NextPage<Props> = function ({ userInfo }) {
const [message, setMessage] = useState('');
const [isAnonymous, setAnonymous] = useState(true);
const [messageList, setMessageList] = useState<InMessage[]>([]);
const toast = useToast();
const { authUser } = useAuth();
async function fetchMessageList(uid: string) {
try {
const resp = await fetch(`/api/message.list?uid=${uid}`);
if (resp.status === 200) {
const data = await resp.json();
setMessageList(data);
}
} catch (err) {
console.log(err);
}
}
useEffect(() => {
if (userInfo === null) return;
fetchMessageList(userInfo.uid);
}, [userInfo]);
if (userInfo === null) {
return <p>사용자를 찾을 수 없습니다.</p>;
}
const isOwner = authUser !== null && authUser.uid === userInfo.uid;
return (
<ServiceLayout title={`${userInfo.displayName}의 홈`} minH="100vh" backgroundColor="gray.50">
<Box maxW="md" mx="auto" pt="6">
<Box borderWidth="1px" borderRadius="lg" overflow="hidden" mb="2" bg="white">
<Flex p="6">
<Avatar size="lg" src={userInfo.photoURL ?? 'https://bit.ly/broken-link'} mr="2" />
<Flex direction="column" justify="center">
<Text fontSize="md">{userInfo.displayName}</Text>
<Text fontSize="xs">{userInfo.email}</Text>
</Flex>
</Flex>
</Box>
<Box borderWidth="1px" borderRadius="lg" overflow="hidden" mb="2" bg="white">
<Flex align="center" p="2">
<Avatar
size="xs"
src={isAnonymous ? 'https://bit.ly/broken-link' : authUser?.photoURL ?? 'https://bit.ly/broken-link'}
mr="2"
/>
<Textarea
bg="gray.100"
border="none"
boxShadow="none !important"
placeholder="어떤이야기를 하고 싶나요?"
borderRadius="md"
resize="none"
minH="unset"
overflow="hidden"
fontSize="xs"
mr="2"
minRows={1}
maxRows={7}
as={ResizeTextarea}
value={message}
onChange={(e) => {
if (e.target.value) {
const lineCount = (e.target.value.match(/[^\n]*\n[^\n]*/gi)?.length ?? 1) + 1;
if (lineCount > 7) {
toast({
title: '최대 7줄까지만 입력가능합니다',
position: 'top-right',
});
return;
}
}
setMessage(e.target.value);
}}
/>
<Button
disabled={message.length === 0}
bgColor="#FFB86C"
color="white"
colorScheme="yellow"
variant="solid"
size="sm"
onClick={async () => {
const postData: {
message: string;
uid: string;
author?: {
displayName: string;
photoURL?: string;
};
} = {
message,
uid: userInfo.uid,
};
if (isAnonymous === false) {
postData.author = {
photoURL: authUser?.photoURL ?? 'https://bit.ly/broken-link',
displayName: authUser?.displayName ?? 'anonymous',
};
}
const messageResp = await postMessage(postData);
if (messageResp.result === false) {
toast({ title: '등록실패', position: 'top-right' });
}
setMessage('');
}}
>
등록
</Button>
</Flex>
<FormControl display="flex" alignItems="center" mt="1" mx="2" pb="2">
<Switch
size="sm"
colorScheme="orange"
id="anonymous"
mr="1"
isChecked={isAnonymous}
onChange={() => {
if (authUser === null) {
toast({
title: '로그인이 필요합니다',
position: 'top-right',
});
return;
}
setAnonymous((prev) => !prev);
}}
/>
<FormLabel htmlFor="anonymous" mb="0" fontSize="xx-small">
Anonymous
</FormLabel>
</FormControl>
</Box>
<VStack spacing="12px" mt="6">
{messageList.map((messageData) => (
<MessageItem
key={`message-item${userInfo.uid}-${messageData.id}`}
item={messageData}
uid={userInfo.uid}
displayName={userInfo.displayName ?? ''}
photoURL={userInfo.photoURL ?? 'https://bit.ly/broken-link'}
isOwner={isOwner}
/>
))}
</VStack>
</Box>
</ServiceLayout>
);
};
export const getServerSideProps: GetServerSideProps<Props> = async ({ query }) => {
const { screenName } = query;
if (screenName === undefined) {
return {
props: {
userInfo: null,
},
};
}
try {
const protocol = process.env.PROTOCOL || 'http';
const host = process.env.HOST || 'localhost';
const port = process.env.PORT || '3000';
const baseUrl = `${protocol}://${host}:${port}`;
const userInfoResp: AxiosResponse<InAuthUser> = await axios(`${baseUrl}/api/user.info/${screenName}`);
return {
props: {
userInfo: userInfoResp.data ?? null,
},
};
} catch (err) {
console.error(err);
return {
props: {
userInfo: null,
},
};
}
};
export default UserHomePage;
回答 1
post하는 경우에 uid 설정
0
138
1
vscode bgColor 바로 표시되기 설정방법 및 chakra-ui img src 설정관련
0
508
2
클래스 방식말고 (2)
0
327
1
클래스 방식말고
0
311
1
배포 후 오류
0
1150
3
const resp = await fetch(`/api/messages.list?uid=${uid}`)가 404에러가 뜹니다
0
438
1
localhost:3000/api/user.info/totuworld 404 에러가 뜹니다.
0
436
3
2. 사용자 API 만들기 => 강의는 짧지만 백엔드 api 만드는 기본 내용 다 들어가 있음 주의하세요!!
0
379
1
toast 처리부터 오류발생
0
599
3
FirebaseError: Firebase: Error (auth/invalid-api-key).
0
1830
3
Vercel 배포 후 thumbnail api 에러 발생합니다.
0
705
1
[~~].toStr에 대해
0
248
1
firebase admin 환경 초기화 하는 부분 질문입니다.
0
405
1
/api/[screenName].ts API에 대해 문의드립니다.
0
339
1
구글 가입 버튼 오류: redirect_uri_mismatch
0
535
1
mac m1 node 14버전 설치 에러
0
642
1
전체 예제 소스좀 올려주세요
0
445
1
R_CONNECTION_TIMED_OUT 오류
0
624
1
로그인 버튼 클릭시 auth/auth-domain-config-required 에러 발생
0
707
1
@types/react를 인식하지 못하는 에러
0
2828
3
thunder client로 get 요청시 무한로딩
0
1154
1
firebase auth 인증 질문
0
1207
1
router 관련 질문입니다.
0
381
1
자동 줄바꿈
0
379
1

