클래스 방식말고
311
投稿した質問数 54
- 학습 관련 질문을 남겨주세요. 상세히 작성하면 더 좋아요!
- 먼저 유사한 질문이 있었는지 검색해보세요.
- 서로 예의를 지키며 존중하는 문화를 만들어가요.
- 잠깐! 인프런 서비스 운영 관련 문의는 1:1 문의하기를 이용해주세요.
설명해주신 싱글톤? 방식말고 평소에 사용하는 방식으로 바꿔봤는데 맞는지 검사가능할까요??
hello.ts
import { NextApiRequest, NextApiResponse } from 'next';
const admin = require('../../model/firebase_admin');
export default function handler(_: NextApiRequest, res: NextApiResponse) {
const db = admin.firestore();
db.collection('test');
res.status(200).json({ name: 'John Doe' });
}modle/firebase_admin.ts
const admin = require('firebase-admin');
interface Config {
credentials: {
privateKey: string;
clientEmail: string;
projectId: string;
};
}
if (!admin.apps.length) {
const config: Config = {
credentials: {
projectId: process.env.projectId || '',
clientEmail: process.env.clientEmail || '',
privateKey: process.env.privateKey?.replace(/\\n/g, '\n') || '',
},
};
admin.initializeApp({
credential: admin.credential.cert(config.credentials),
});
console.info('bootstrap firebase admin');
}
module.exports = admin;현재 firestore만 hello.ts에서 test 보내는 방식만 따라해봤습니다!

질문 1. 이 방식으로 사용해도 될까요??
이 방식으로 문제될게 있을까요?
질문 2. 아래 코드가 출력이 안되는데 이유가 있을까요?
console.info('bootstrap firebase admin');위 전체코드에서처럼 if문안에 작성하게되면 콘솔이 영상처럼 터미널에 안뜨는데 왜그런걸까요?
if문 밖에 선언하면 잘 출력됩니다.
回答 1
0
안녕하세요 oriori2705님!
작성해주신 코드 잘 동작할듯해요.
admin에 앱이 있는지 체크하는 부분이 있잖아요.
if (!admin.apps.length) {다만, 이 코드는 require로 가져오도록 하는데요.
이게 여러 파일에 걸쳐있을 때, 어플리케이션이 실행되면 한번만 init 이 되는지 확인해봐야겠네요.
2번 질문은 admin.apps.length가 어떤 값을 가지는지 확인해보셔야할 듯하네요.
if 문안에서 동작안한다면 if문 코드가 실행되지 않았을 가능성이 있어요.
post하는 경우에 uid 설정
0
138
1
vscode bgColor 바로 표시되기 설정방법 및 chakra-ui img src 설정관련
0
508
2
클래스 방식말고 (2)
0
327
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
사용자를 찾을 수 없다.
0
378
1
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

