경고, FIREBASE_CONFIG 환경 변수가 없습니다.firebase-admin 초기화가 실패합니다.
Firebase 문서를 따르지만 Firebase에 연결할 수 없는 경우.
이 문제에 직면한 사람이 있습니까?
내 코드:
index.js
var admin = require("firebase-admin"); var serviceAccount = require("./key/serviceAccountKey.json"); admin.initializeApp({ credential: admin.credential.cert(serviceAccount), databaseURL: "https://sampleLink.firebaseio.com", }); const functions = require("firebase-functions"); var db = admin.firestore(); var data = { name: "Los Angeles", state: "CA", country: "USA", }; // Add a new document in collection "cities" with ID 'LA' var setDoc = db.collection("cities").doc("LA").set(data); setDoc;
package.json
{ "name": "functions", "description": "Cloud Functions for Firebase", "scripts": { "serve": "firebase serve --only functions", "shell": "firebase functions:shell", "start": "npm run shell", "deploy": "firebase deploy --only functions", "logs": "firebase functions:log" }, "dependencies": { "firebase": "^5.7.3", "firebase-admin": "~6.0.0", "firebase-functions": "^2.1.0" }, "private": true }
console
tktktk:functions dev$ node index.js Warning, FIREBASE_CONFIG environment variable is missing. Initializing firebase-admin will fail The behavior for Date objects stored in Firestore is going to change AND YOUR APP MAY BREAK. To hide this warning and ensure your app does not break, you need to add the following code to your app before calling any other Cloud Firestore methods: const firestore = new Firestore(); const settings = {/* your settings... */ timestampsInSnapshots: true}; firestore.settings(settings); With this change, timestamps stored in Cloud Firestore will be read back as Firebase Timestamp objects instead of as system Date objects. So you will also need to update code expecting a Date to instead expect a Timestamp. For example: // Old: const date = snapshot.get('created_at'); // New: const timestamp = snapshot.get('created_at'); const date = timestamp.toDate(); Please audit all existing usages of Date when you enable the new behavior. In a future release, the behavior will change to the new behavior, so if you do not follow these steps, YOUR APP MAY BREAK.
내 환경은 다음과 같습니다.
$ firebase --version
6.3.0
$ node -v
v8.12.0
$ npm -v
6.4.1
그거FIREBASE_CONFIG
경고는 로의 경로를 암시합니다.JSON
누락(또는 잘못됨).
설정 중 하나FIREBASE_CONFIG
필요에 따라 - 또는 설정GOOGLE_APPLICATION_CREDENTIALS
환경 변수로 지정한 후 실행gcloud auth application-default login
그러면 당신은 사용할 수 있습니다.admin.credential.applicationDefault()
대신에admin.credential.cert(serviceAccount)
.
클라우드 기능을 로컬에서 실행하는 올바른 방법은 Functions 셸을 사용하는 것입니다(https://firebase.google.com/docs/functions/local-emulator) 참조).다음과 같이 직접 실행하려면$ node index.js
필요한 환경 변수를 직접 설정해야 합니다. 그렇지 않으면firebase-functions
불평할 것입니다.
그러나 위의 메시지는 경고에 불과합니다.그럼에도 불구하고 당신의 코드는 실행되고 있습니다.Firestore에 기록된 데이터가 없으면 Firestore에서 반환한 약속을 처리하지 않기 때문일 수 있습니다.set()
방법.
저에게 문제는 다음과 같은 대사였습니다.
const functions = require('firebase-functions');
방금 index.js에서 삭제했습니다.저는 이제 파이어베이스 클라우드 기능을 사용하지 않지만, 그 라인을 제거하는 것을 잊었습니다.이것이 제 문제의 근원입니다.
그래서 당신도 이 패키지를 필요 없는 파일로 가져왔을 수도 있습니다.
저도 똑같은 문제가 있었습니다.노드 버전 10과 관련이 있는 것으로 밝혀졌습니다. 노드 10을 제거하고 노드 8로 돌아가서 모든 것이 매력적으로 작동했습니다.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
그냥 치기
firebase deploy --only functions
노드 v8에 있습니다.
당신의 문제에 대해서, 방금 당신의 코드 덕분에 저는 제 문제를 해결할 수 있었습니다.
내가 하는 일:
const admin = require('firebase-admin');
var serviceAccount = require("./serviceAccount.json");
admin.initializeApp({ credential: admin.credential.cert(serviceAccount) });
const db = admin.firestore();
저는 실시간 db가 아닌 클라우드 파이어스토어 db를 사용하고 있기 때문에 initializeApp에서 두 번째 인수를 보내지 않고 db의 구성을 환경 파일에 넣었습니다.
export const environment = {
production: false,
firebaseConfig : {
apiKey: 'XXXXXXXXXXX',
authDomain: 'XXXXXXXXXX',
databaseURL: 'XXXXXXXXXX',
projectId: 'XXXXXXXXX',
storageBucket: 'XXXXXXXXXXX',
messagingSenderId: 'XXXXXXXXXXX',
appId: 'XXXXXXXXXXXX',
measurementId: 'XXXXXXX'
}
};
이것은 저에게 효과가 있었습니다.
admin.initializeApp({
credential: admin.credential.cert({
"type": "service_account",
"project_id": "took from Firebase Generated Private Key",
"private_key_id": "took from Firebase Generated Private Key",
"private_key": "took from Firebase Generated Private Key",
"client_email": "took from Firebase Generated Private Key",
"client_id": "took from Firebase Generated Private Key",
"auth_uri": "took from Firebase Generated Private Key",
"token_uri": "took from Firebase Generated Private Key",
"auth_provider_x509_cert_url": "took from Firebase Generated Private Key",
"client_x509_cert_url": "took from Firebase Generated Private Key"
}),
databaseURL: "https://test-dia.firebaseio.com"
});
2021년 현재 경고를 제거하는 올바른 방법은 다음과 같습니다.firebase-functions-test
다음과 같이 joke 구성으로 호출합니다.
// jest.preset.js
const test = require('firebase-functions-test')();
// ...
"테스트 모드"에서 테스트하는 방법에 대한 자세한 내용은 https://firebase.google.com/docs/functions/unit-testing#offline-mode 에서 확인하십시오. https://firebase.google.com/docs/functions/unit-testing#offline-mode
클라우드 기능을 로컬에서 실행하려고 하고 클라우드 인증 및 데이터베이스를 사용하려는 경우.
단계: 1 - Firebase 콘솔로 이동하여 자격 증명 데이터를 JSON으로 복사합니다. 예:
{
"apiKey": "<YOUR-API-KEY>",
"authDomain": "<YOUR-authDomain>",
"databaseURL": "<YOUR-databaseURL>",
"projectId": "...",
"storageBucket": "...",
"messagingSenderId": "...",
"appId": "..."
}
2 - 프로젝트 폴더에 JSON 파일로 저장합니다.예를 들어 'my_credentials.json'입니다.
3 - 로컬로 기능 실행: (cat 참고)
FIREBASE_CONFIG=$(cat my_contentials.json) npm 로컬 실행
참고(!):패키지에 '로컬' 스크립트를 추가합니다.json 파일:
"scripts": {
...
"local": "npx functions-framework --target=<YOUR-APP-NAME> --signature-type=<YOUR-FUNCTION-CALL-METHOD-EXAMPLE-http>"
},
종속성을 이버전에종시사도용서성속에()시▁try도서in).package.json
v8.(v8로 표시됨).
"dependencies": {
"firebase-admin": "~7.1.1",
"firebase-functions": "^2.2.1"
}
그것은 나에게 효과가 있었다.
언급URL : https://stackoverflow.com/questions/54292448/warning-firebase-config-environment-variable-is-missing-initializing-firebase
'itsource' 카테고리의 다른 글
null 값을 포함하는 열에 null이 아닌 제약 조건을 추가하는 방법 (0) | 2023.06.11 |
---|---|
메이븐2: 사라진 유물이지만 항아리가 제자리에 있습니다. (0) | 2023.06.11 |
문자열에서 선행 및 후행 0을 제거하는 방법은 무엇입니까?파이썬 (0) | 2023.06.11 |
다중 처리:여러 프로세스에서 딕트를 공유하려면 어떻게 해야 합니까? (0) | 2023.06.11 |
ggplot2를 사용하여 축에 정수 값만 표시하는 방법 (0) | 2023.06.11 |