LoginSignup
0
0

More than 1 year has passed since last update.

Firestoreでサーバーのタイムスタンプを設定する時はフィールド名をtimestampにする

Last updated at Posted at 2021-12-01

起きたこと

Firestoreのドキュメント追加処理を下記の様に書きました。

"firebase": "^9.5.0"
usersはコレクションの名前です。
※省略部分で分からない事があれば聞いてください。

import { doc, setDoc, serverTimestamp } from "firebase/firestore";
// 省略
const userDocRef = doc(firestore, "users", "userId");
await setDoc(userDocRef, {
    ...userDocData,
    updatedAt: serverTimestamp(),
});

すると、こんなエラーが出ました。

Unhandled Rejection (FirebaseError): Function setDoc() called with invalid data Unsupported field value: a function (found in field updatedAt in document users/xxx...)

解決方法

フィールド名をtimestampにする。

import { doc, setDoc, serverTimestamp } from "firebase/firestore";
// 省略
const userDocRef = doc(firestore, "users", "userId");
await setDoc(userDocRef, {
    ...userDocData,
    timestamp: serverTimestamp(),
});

以上、経験が浅く迷ってしまったため書き留めました。公式に素直に従うのは大事ですね。

参考

Firestore入門ガイド

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