1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

QueryDocumentSnapShotとDocumentSnapShotを厳密に区別する必要はない

Posted at
const doc = await db.collection(hoge).doc(id).get();

const query = await db.collection(hoge).get();
query.forEach(doc => {
  
});

上記の二つのdocの方は厳密には前者がDocumentSnapShotで後者がQueryDocumentSnapShotである。

では、これらのdocを同じ変数や引数で受けるために

const someFunc(doc:firestore.QueryDocumentSnapShot | firestore.DocumentSnapShot) => {
}

としなければならないのかと言うと、特にそんなことはない。

QueryDocumentSnapShotはDocumentSnapShotのサブクラスであり、
公式ドキュメントにも、違いはQueryDocumentSnapShotの場合isExistsが常にtrueを返したり、
getDataがundefinedを返すことはない程度であると書いてある。

typescriptで型指定する場合は単にfirestore.DocumentSnapShotでOK。

1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?