티스토리 뷰
cloud functions 업데이트 - 1. acquiring application default credentials 오류 ??2. doc.data() is not a function
민동그라미 2018. 4. 7. 10:14베타버전이던 cloud functions가 버전 업을 하였다.
따라서 .1. npm install --save firebase-functions@latest 해주고
2. https://firebase.google.com/docs/functions/beta-v1-diff
위의 페이지에서 바뀐 부분을 확인하고 코드를 수정해주어야한다.
1) firebase-admin 버전 업그레이드 하기
SDK version 1.0.0 requires firebase-admin
5.11.0.
package.json 가서
firebase-admin ^5.8.1을 ^5.11.0 으로!
(^의 의미도 알아야겠다... https://stackoverflow.com/questions/22137778/what-does-mean-in-package-json-versioning 내용에는 ~을 권장한다던데 일단 ^로 써보아야겠다.)
그냥 공식홈페이지에 나와있는 것처럼 아래 것을 시행한다.
npm install firebase-functions@latest --save
npm install firebase-admin@5.11.0 --save
2) 위를 사용하는데는 Firebase CLI 를 사용하고 이에 대한 Version 3.18.0 을 원할하게 이용하기 위해서는
npm install -g firebase-tools
본인은 npm upgrade -g firebase-tools를 이용
3) 부분적으로 코드 수정
> 영향을 미치는 코드
1) event.data() 등 ,다음에 것이 data 와 context로 수정이 됨
2) firebase- admin initialize 부분
3) functions.config().firebase 가 없어진다!
>영향을 미치는 api
구글 실시간 데이터베이스
cloud firestore
authentication
crashlytics
storage
> 내부적 코드 수정
따라서 cloud functions 기능을 사용하면 변경부분 이벤트를 trigger 시킬 때
exports.blurOffensiveImages = functions.storage.object().onChange((event) => {
(event) 하나의 파라미터 역시 data, context로 바뀌고
사용되는 변수 사용들 역시 바뀌었다.
이를 확인할 수 있는 링크
https://firebase.google.com/docs/functions/beta-v1-diff#changes_by_trigger_type
//중간부분부터
실시간 데이터베이스의 예
exports.dbWrite = functions.database.ref('/path/with/{id}').onWrite((data, context) => {
const authVar = context.auth; // Auth information for the user.
const authType = context.authType; // Permissions level for the user.
const pathId = context.params.id; // The ID in the Path.
const eventId = context.eventId; // A unique event ID.
const timestamp = context.timestamp; // The timestamp at which the event happened.
const eventType = context.eventType; // The type of the event that triggered this function.
const resource = context.resource; // The resource which triggered the event.
// ...
});
1>> admin 을 이용한 코드 수정
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
해주던 것들
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
로 수정
2>> 사용하는 api 에 맞게 코드 수정
https://firebase.google.com/docs/reference/functions/functions.storage.ObjectMetadata