質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Firebase

Firebaseは、Googleが提供するBasSサービスの一つ。リアルタイム通知可能、並びにアクセス制御ができるオブジェクトデータベース機能を備えます。さらに認証機能、アプリケーションのログ解析機能などの利用も可能です。

Google Cloud Platform

Google Cloud Platformは、Google社がクラウド上で提供しているサービス郡の総称です。エンドユーザー向けサービスと同様のインフラストラクチャーで運営されており、Webサイト開発から複雑なアプリ開発まで対応可能です。

Cloud Firestore

Cloud Firestore は、自動スケーリングと高性能を実現し、アプリケーション開発を簡素化するように構築された NoSQLドキュメントデータベースです。

Google

Googleは、アメリカ合衆国に位置する、インターネット関連のサービスや製品を提供している企業です。検索エンジンからアプリケーションの提供まで、多岐にわたるサービスを提供しています。

Q&A

解決済

1回答

5377閲覧

cloud functionsのエラー「Error: Process exited with code 16」が発生する原因は?

Gento

総合スコア77

Firebase

Firebaseは、Googleが提供するBasSサービスの一つ。リアルタイム通知可能、並びにアクセス制御ができるオブジェクトデータベース機能を備えます。さらに認証機能、アプリケーションのログ解析機能などの利用も可能です。

Google Cloud Platform

Google Cloud Platformは、Google社がクラウド上で提供しているサービス郡の総称です。エンドユーザー向けサービスと同様のインフラストラクチャーで運営されており、Webサイト開発から複雑なアプリ開発まで対応可能です。

Cloud Firestore

Cloud Firestore は、自動スケーリングと高性能を実現し、アプリケーション開発を簡素化するように構築された NoSQLドキュメントデータベースです。

Google

Googleは、アメリカ合衆国に位置する、インターネット関連のサービスや製品を提供している企業です。検索エンジンからアプリケーションの提供まで、多岐にわたるサービスを提供しています。

0グッド

0クリップ

投稿2021/02/01 11:52

編集2021/02/01 13:06

#cloud functions謎のエラーの原因は?

console

1Error: Process exited with code 16 2 at process.on.code (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:275:22) 3 at process.emit (events.js:198:13) 4 at process.EventEmitter.emit (domain.js:448:20) 5 at process.exit (internal/process/per_thread.js:168:15) 6 at Object.sendCrashResponse (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/logger.js:37:9) 7 at process.on.err (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:271:22) 8 at process.emit (events.js:198:13) 9 at process.EventEmitter.emit (domain.js:448:20) 10 at emitPromiseRejectionWarnings (internal/process/promises.js:140:18) 11 at process._tickCallback (internal/process/next_tick.js:69:34)

cloud functionsでmonthCountEvents関数をデプロイすると、monthCountEventsが発動するたびにこのようなエラーが発生します。

1

このエラーの原因は何でしょうか?

#何を実現したいか?
Firebase 要素の数をカウントするなら、最初からこの方法でいきましょう!(Cloud Functions)
☝️上記の記事を参考に、Firestoreのドキュメントの数をカウントしています。

users/{userId}/events/{eventId}のドキュメントに変化があった場合、Firestore内のドキュメントフィールド2箇所にカウントを記録します。

typescript

1exports.monthCountEvents = functions.region('asia-northeast1').firestore 2 .document('users/{userId}/events/{eventId}') 3 .onWrite((change, context) => {

#該当のコード
ほぼ同じようなコードでも、エラーが**「発生しないパターン」「発生するパターン」**があります。。。😟

##エラーが発生しないパターン

typescript:functions/index.ts

1import * as functions from 'firebase-functions'; 2import * as admin from 'firebase-admin'; 3admin.initializeApp(); 4const db = admin.firestore(); 5 6// // Start writing Firebase Functions 7// // https://firebase.google.com/docs/functions/typescript 8 9// eventが作成された時にカウントする 10exports.countEvents = functions.region('asia-northeast1').firestore 11 .document('users/{userId}/events/{eventId}') 12 .onWrite((change, context) => { 13 14 let periods: boolean; 15 let dayEventCounts: number; 16 if (!change.before.exists) { 17 periods = change.after.get('periods'); 18 dayEventCounts = change.after.get('dayEventCounts'); 19 } else { 20 periods = change.before.get('periods'); 21 dayEventCounts = change.before.get('dayEventCounts'); 22 } 23 24 const userId = context.params.userId; 25 const FieldValue = admin.firestore.FieldValue; 26 const countsRef = db.collection('users').doc(userId); 27 28 if (!change.before.exists && !periods) { 29 // 登録時に件数をインクリメント 30 countsRef.update({ eventCount: FieldValue.increment(dayEventCounts) }); 31 return 32 } else if (change.before.exists && !change.after.exists && !periods) { 33 // 削除時に件数をデクリメント 34 countsRef.update({ eventCount: FieldValue.increment(-dayEventCounts) }); 35 return 36 } 37 return; 38 }); 39 40exports.monthCountEvents = functions.firestore.document('users/RmSJbxK2A2QNWZbm2nLQwEr7tNw2/events/{eventId}') 41 .onWrite((change) => { 42 let pushMonths: any; 43 let periods: boolean; 44 let dayEventCounts: number; 45 if (!change.before.exists) { 46 pushMonths = change.after.get('pushMonths'); 47 periods = change.after.get('periods'); 48 dayEventCounts = change.after.get('dayEventCounts'); 49 } else { 50 pushMonths = change.before.get('pushMonths'); 51 periods = change.before.get('periods'); 52 dayEventCounts = change.before.get('dayEventCounts'); 53 } 54 55 const FieldValue = admin.firestore.FieldValue; 56 const countsRef = db.collection(`users/RmSJbxK2A2QNWZbm2nLQwEr7tNw2/counts`).doc(pushMonths); 57 countsRef.get().then((docSnapshot) => { 58 if (!docSnapshot.exists) { 59 countsRef.set({ monthCount: 1, monthDate: pushMonths }); 60 } 61 }); 62 63 if (!change.before.exists && !periods) { 64 // 登録時に件数をインクリメント 65 return countsRef.update({ monthCount: FieldValue.increment(dayEventCounts) } ); 66 } else if (change.before.exists && !change.after.exists && !periods) { 67 // 削除時に件数をデクリメント 68 return countsRef.update({ monthCount: FieldValue.increment(-dayEventCounts) }); 69 } 70 return 71 })

このmonthCountEventsはなんのエラーもなく発動します。
これはテスト用なので、自分のuidを指定してonWriteがトリガーするようにしています

##エラーが発生するパターン

typescript:functions/index.ts

1import * as functions from 'firebase-functions'; 2import * as admin from 'firebase-admin'; 3admin.initializeApp(); 4const db = admin.firestore(); 5 6// // Start writing Firebase Functions 7// // https://firebase.google.com/docs/functions/typescript 8 9// eventが作成された時にカウントする 10exports.countEvents = functions.region('asia-northeast1').firestore 11 .document('users/{userId}/events/{eventId}') 12 .onWrite((change, context) => { 13 14 let periods: boolean; 15 let dayEventCounts: number; 16 if (!change.before.exists) { 17 periods = change.after.get('periods'); 18 dayEventCounts = change.after.get('dayEventCounts'); 19 } else { 20 periods = change.before.get('periods'); 21 dayEventCounts = change.before.get('dayEventCounts'); 22 } 23 24 const userId = context.params.userId; 25 const FieldValue = admin.firestore.FieldValue; 26 const countsRef = db.collection('users').doc(userId); 27 28 if (!change.before.exists && !periods) { 29 // 登録時に件数をインクリメント 30 countsRef.update({ eventCount: FieldValue.increment(dayEventCounts) }); 31 return 32 } else if (change.before.exists && !change.after.exists && !periods) { 33 // 削除時に件数をデクリメント 34 countsRef.update({ eventCount: FieldValue.increment(-dayEventCounts) }); 35 return 36 } 37 return; 38 }); 39 40exports.monthCountEvents = functions.region('asia-northeast1').firestore 41 .document('users/{userId}/events/{eventId}') 42 .onWrite((change, context) => { 43 44 let pushMonths: any; 45 let periods: boolean; 46 let dayEventCounts: number; 47 if (!change.before.exists) { 48 pushMonths = change.after.get('pushMonths'); 49 periods = change.after.get('periods'); 50 dayEventCounts = change.after.get('dayEventCounts'); 51 } else { 52 pushMonths = change.before.get('pushMonths'); 53 periods = change.before.get('periods'); 54 dayEventCounts = change.before.get('dayEventCounts'); 55 } 56 57 const userId = context.params.userId; 58 const FieldValue = admin.firestore.FieldValue; 59 const monthCountRef = db.collection(`users/${userId}/counts`).doc(pushMonths); 60 monthCountRef.get().then((docSnapshot) => { 61 if (!docSnapshot.exists) { 62 monthCountRef.set({ monthCount: 1, monthDate: pushMonths }); 63 } 64 }); 65 66 if (!change.before.exists && !periods) { 67 // 登録時に件数をインクリメント 68 return monthCountRef.update({ monthCount: FieldValue.increment(dayEventCounts) }); 69 } else if (change.before.exists && !change.after.exists && !periods) { 70 // 削除時に件数をデクリメント 71 return monthCountRef.update({ monthCount: FieldValue.increment(-dayEventCounts) }); 72 } 73 return; 74 });

このmonthCountEventsは本番用なので、自分のuidではなくワイルドカードを使い、onWriteをトリガーしています。

何が違って、何が原因なのかさっぱりわかりません。。。
どなたかご教授ください!!

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

こちらの通り、.catch()を追加すると謎のエラーは出なくなりました!

投稿2021/02/02 13:27

Gento

総合スコア77

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問