๐ค ๋ฌธ์ ์ํฉ
React ํ๋ก์ ํธ์์ Firebase Firestore๋ฅผ ์ฌ์ฉํ์ฌ ์ฌ์ฉ์ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ค๋ ๊ณผ์ ์์ ์ฌ๋ฏธ์๋ ํ์์ ๋ฐ๊ฒฌํ์ต๋๋ค. ์๋ฅผ ์๋ฃํ ์ฌ์ฉ์๋ค์ ๋๋ค์์ ํ์ํ๋ ๊ธฐ๋ฅ์ ๊ตฌํํ๋ ์ค, ๋ช๋ช ์์์ ์ฒซ ๋ฒ์งธ ์ฌ์ฉ์์ ๋๋ค์์ด ํ์๋์ง ์๋ ๋ฌธ์ ๊ฐ ์์์ต๋๋ค.
๋ฌธ์ ํด๊ฒฐ์ ์ํด ์ฝ์ ๋ก๊ทธ๋ฅผ ์ถ๊ฐํ๋๋ฐ, ๋๋๊ฒ๋ ๋ก๊ทธ๋ฅผ ์ถ๊ฐํ๋ ๊ฒ๋ง์ผ๋ก ๋ฌธ์ ๊ฐ ํด๊ฒฐ๋์์ต๋๋ค!
๐ ์์ธ ๋ถ์
์๋ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์์ต๋๋ค:
const fetchUserNicknames = async (userIds: string\[\]) => {
if (!userIds.length) return;
try {
const nicknames: { \[key: string\]: string } = {};
for (const userId of userIds) {
if (nicknames\[userId\]) continue;
const userDoc = await getDoc(doc(db, 'users', userId));
if (userDoc.exists()) {
const userData = userDoc.data();
nicknames\[userId\] = userData.nickname || userData.displayName || userId.substring(0, 8);
} else {
nicknames\[userId\] = userId.substring(0, 8);
}
}
setCompletedUserNames(nicknames);
} catch (error) {
console.error('์ฌ์ฉ์ ๋๋ค์ ๊ฐ์ ธ์ค๊ธฐ ์ค๋ฅ:', error);
}
};
์ด ์ฝ๋์ ๋ฌธ์ ์ ์:
- ์์ฐจ์ ์ผ๋ก ๋น๋๊ธฐ ์์ฒญ์ ์ฒ๋ฆฌํ๋ฉด์ ๊ฐ ์์ฒญ ์ฌ์ด์ ํ์ด๋ฐ ์ด์๊ฐ ๋ฐ์ํ ์ ์์์ต๋๋ค.
- ์ฝ์ ๋ก๊ทธ๋ฅผ ์ถ๊ฐํ์ ๋ ๋ฌธ์ ๊ฐ ํด๊ฒฐ๋ ๊ฒ์, ๋ก๊ทธ ์ถ๋ ฅ์ผ๋ก ์ธํด ๊ฐ ๋น๋๊ธฐ ์์ ์ฌ์ด์ ๋ฏธ์ธํ ์๊ฐ ๊ฐ๊ฒฉ์ด ์๊ฒผ๊ธฐ ๋๋ฌธ์ ๋๋ค.
์ฌ์ค ์์ ์๋ ์ข ์ข ๊ฒช์์ง๋ง ๋ฌธ์ ๊ฐ ํด๊ฒฐ๋๋ฉด ๋ฐ๋ก ๋๊ธฐ๊ณค ํ๋๋ฐ,
์กฐ๊ธ ๊ฒ์ํด์ ์ฐพ์๋ณด๋ ์ด๋ฅผ "Heisenbug"๋ผ๊ณ ๋ถ๋ฆฌ๋ ๊ฑธ ์์์ต๋๋ค.
https://en.wikipedia.org/wiki/Heisenbug
Heisenbug - Wikipedia
From Wikipedia, the free encyclopedia Software bug that seems to change when debugging For the Linux distribution codenamed Heisenbug, see Fedora Linux. In computer programming jargon, a heisenbug is a software bug that seems to disappear or alter its beha
en.wikipedia.org
Heisenbug : ๋ฒ๊ทธ๋ฅผ ๊ด์ฐฐํ๋ ค๋ ํ์(๋ก๊ทธ ์ถ๊ฐ)๊ฐ ๋ฒ๊ทธ์ ๋์์ ๋ณ๊ฒฝ์ํค๋ ํ์
โจ ํด๊ฒฐ ๋ฐฉ๋ฒ
๋ ์์ ์ ์ธ ํด๊ฒฐ์ ์ํด `Promise.all()`์ ์ฌ์ฉํ์ฌ ๋ชจ๋ ๋น๋๊ธฐ ์์ฒญ์ ๋ณ๋ ฌ๋ก ์ฒ๋ฆฌํ๋๋ก ์์ ํ์ต๋๋ค:
const fetchUserNicknames = async (userIds: string\[\]) => {
if (!userIds.length) return;
try {
const nicknames: { \[key: string\]: string } = {};
// Promise.all์ ์ฌ์ฉํ์ฌ ๋ชจ๋ ์์ฒญ์ ๋ณ๋ ฌ๋ก ์ฒ๋ฆฌ
await Promise.all(
userIds.map(async (userId) => {
if (nicknames\[userId\]) return;
const userDoc = await getDoc(doc(db, 'users', userId));
if (userDoc.exists()) {
const userData = userDoc.data();
nicknames\[userId\] = userData.nickname || userData.displayName || userId.substring(0, 8);
} else {
nicknames\[userId\] = userId.substring(0, 8);
}
})
);
setCompletedUserNames(nicknames);
} catch (error) {
console.error('์ฌ์ฉ์ ๋๋ค์ ๊ฐ์ ธ์ค๊ธฐ ์ค๋ฅ:', error);
}
};
์์ ํ์๋,
- ๋ชจ๋ ์ฌ์ฉ์ ์ ๋ณด ์์ฒญ์ด ๋ณ๋ ฌ๋ก ์ฒ๋ฆฌ๋ฉ๋๋ค.
- ๊ฐ ์์ฒญ์ด ๋ ๋ฆฝ์ ์ผ๋ก ์ฒ๋ฆฌ๋์ด ํ ์์ฒญ์ ์ง์ฐ์ด ๋ค๋ฅธ ์์ฒญ์ ์ํฅ์ ๋ฏธ์น์ง ์์ต๋๋ค.
- ๋ชจ๋ ์์ฒญ์ด ์๋ฃ๋ ํ์ ํ ๋ฒ์ ์ํ๋ฅผ ์ ๋ฐ์ดํธํฉ๋๋ค.
๐ก ๊ตํ
- ๋น๋๊ธฐ ์์ ์ ๋ค๋ฃฐ ๋๋ ์์ฐจ ์ฒ๋ฆฌ์ ๋ณ๋ ฌ ์ฒ๋ฆฌ์ ์ฐจ์ด๋ฅผ ์ดํดํ๊ณ ์ํฉ์ ๋ง๊ฒ ์ฌ์ฉํ๊ธฐ
- ์ฝ์ ๋ก๊ทธ๋ก ๋ฌธ์ ๊ฐ ํด๊ฒฐ๋๋ ๊ฒ์ฒ๋ผ ๋ณด์ธ๋ค๋ฉด, ๊ทธ๊ฒ์ ํ์ด๋ฐ ์ด์์ผ ๊ฐ๋ฅ์ฑ์ด ๋์!
- `Promise.all()` ์ฌ๋ฌ ๋น๋๊ธฐ ์์ ํ ๋ ์ฌ์ฉํ๊ธฐ
- "Heisenbug" ํ์์ด๋ผ๋ ๊ฒ ์์ ์ ์๋ค ~~
๋๊ธ