Categories: firebase

[firebase] TypeError: firestoreInstance.settings is not a function の対処法

最近ニートになったので、firebaseをひたすら弄ってます。

こんにちは〜もぐめっとです。

ある雨の日のこと、cloud functionにdeployしようとしたらこんなエラーが突然出た。

% firebase deploy –only function

┌──────────────────────────────────────────────┐
│ Update available: 4.0.3 (current: 3.19.3) │
│ Run npm install -g firebase-tools to update. │
└──────────────────────────────────────────────┘

⚠ functions: package.json indicates an outdated version of firebase-functions.
Please upgrade using npm install –save firebase-functions@latest in your functions directory.

Error: Cannot understand what targets to deploy. Check that you specified valid targets if you used the –only or –except flag. Otherwise, check your firebase.json to ensure that your project is initialized for the desired features.

どうやらfirebase-functionが新しくなったらしいので以下のコマンドで新しくした

$ npm install --save firebase-functions@latest

そんで、デプロイしたあと動かしてみたらこんなエラーが出るようになってしまった。

TypeError: firestoreInstance.settings is not a function
at snapshotConstructor (/user_code/node_modules/firebase-functions/lib/providers/firestore.js:124:27)
at cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:108:28)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:139:20)
at /var/tmp/worker/worker.js:730:24
at process._tickDomainCallback (internal/process/next_tick.js:135:7)

ので、これの解決法を記す。

答えは以下にあった

functions.config().firebaseなくなったんかーーーい!
そんで初期化の方法は以下のように変わったらしい。

以前(v0.9.1 以前)

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

現在(v1.0.0)

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

そんでもって、firebase-adminも更新する必要がありそうだったのでついでに更新。

$ npm install --save firebase-admin@latest

cf: Firebase Cloud Functions – context params reference brings error: firestoreInstance.settings is not a function at snapshotConstructor

After updating firebase-admin to the latest version (5.13.1) it’s working correctly. It seems that firebase-functions v2 need firebase-admin version 5.13.0 or above !

そんで改めてデプロイしたら無事動きました!めでたしめでたし。

P.S.
余談ですがcloud functionで、node.js 8がつかえるようになったらしいです。

https://dev.to/jgs/firebase-functions--nodejs-8--23lk からクロスポストです。tldrfirebase-tools をアップグレードするfunctions/pa...
Firebase Functions で Node.js 8 使えるようになったぞ〜〜〜!!! - Qiita - Qiita
mogmet