0
0

More than 1 year has passed since last update.

【Node】CloudFunctionsでincrement/decrementしたい

Posted at

はじめに

Firebase CloudFunctionsでフィールド値をインクリメントしようとしてちょいハマりかけたので備忘録

更新履歴

2022.8.27 初回投稿

環境

  • macOS Monterey 12.5.1
  • Node:v18.7.0
  • npm:v8.18.0

参考にしたサイト

やりたいこと

  • Cloud FunctionsのFirestoreの特定のフィールドでincrement/decrementしたい

サンプル

  • incrementサンプル
const db = admin.firestore();
const userRef = db.collection('documents').doc('document_id');
const increment = admin.firestore.FieldValue.increment(1);//admin.firestore()ではない

userRef.update({ FieldToIncrease: increment });
  • decrementサンプル
const db = admin.firestore();
const userRef = db.collection('documents').doc('document_id');
const increment = admin.firestore.FieldValue.increment(-1);//decrementは-をつける

userRef.update({ FieldToDecrease: increment });

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0