A DocumentSnapshot is an immutable representation for a document in a Firestore database. The data can be extracted with [data()] or [get(fieldPath)] to get a specific field.
For a DocumentSnapshot that points to a non-existing document, any data access will return 'undefined'. You can use the [exists] property to explicitly verify a document's existence.
DocumentSnapshot
Package
@google-cloud/firestoreConstructors
(constructor)(ref, _fieldsProto, readTime, createTime, updateTime)
constructor
(
ref
:
DocumentReference<T>
,
_fieldsProto
?:
ApiMapValue
|
undefined
,
readTime
?:
Timestamp
,
createTime
?:
Timestamp
,
updateTime
?:
Timestamp
);
Constructs a new instance of the DocumentSnapshot
class
ref
_fieldsProto
ApiMapValue
| undefined
The fields of the Firestore Document
Protobuf backing this document (or undefined if the document does not exist).
readTime
createTime
updateTime
Timestamp
The time when the document was last updated (or undefined if the document does not exist).
Properties
_fieldsProto
readonly
_fieldsProto
?:
ApiMapValue
|
undefined
;
createTime
get
createTime
()
:
Timestamp
|
undefined
;
The time the document was created. Undefined for documents that don't exist.
{Timestamp|undefined} DocumentSnapshot#createTime
let
documentRef
=
firestore
.
doc
(
'col/doc'
);
documentRef
.
get
().
then
(
documentSnapshot
=
>
{
if
(
documentSnapshot
.
exists
)
{
let
createTime
=
documentSnapshot
.
createTime
;
console
.
log
(
`Document created at '
${
createTime
.
toDate
()
}
'`
);
}
});
exists
get
exists
()
:
boolean
;
True if the document exists.
{boolean} DocumentSnapshot#exists
let
documentRef
=
firestore
.
doc
(
'col/doc'
);
documentRef
.
get
().
then
((
documentSnapshot
)
=
>
{
if
(
documentSnapshot
.
exists
)
{
console
.
log
(
`Data:
${
JSON
.
stringify
(
documentSnapshot
.
data
())
}
`
);
}
});
id
get
id
()
:
string
;
The ID of the document for which this DocumentSnapshot contains data.
{string} DocumentSnapshot#id
let
documentRef
=
firestore
.
doc
(
'col/doc'
);
documentRef
.
get
().
then
((
documentSnapshot
)
=
>
{
if
(
documentSnapshot
.
exists
)
{
console
.
log
(
`Document found with name '
${
documentSnapshot
.
id
}
'`
);
}
});
readTime
get
readTime
()
:
Timestamp
;
The time this snapshot was read.
{Timestamp} DocumentSnapshot#readTime
let
documentRef
=
firestore
.
doc
(
'col/doc'
);
documentRef
.
get
().
then
(
documentSnapshot
=
>
{
let
readTime
=
documentSnapshot
.
readTime
;
console
.
log
(
`Document read at '
${
readTime
.
toDate
()
}
'`
);
});
ref
get
ref
()
:
DocumentReference<T>
;
A [DocumentReference] DocumentReference for the document stored in this snapshot.
{DocumentReference} DocumentSnapshot#ref
let
documentRef
=
firestore
.
doc
(
'col/doc'
);
documentRef
.
get
().
then
((
documentSnapshot
)
=
>
{
if
(
documentSnapshot
.
exists
)
{
console
.
log
(
`Found document at '
${
documentSnapshot
.
ref
.
path
}
'`
);
}
});
updateTime
get
updateTime
()
:
Timestamp
|
undefined
;
The time the document was last updated (at the time the snapshot was generated). Undefined for documents that don't exist.
{Timestamp|undefined} DocumentSnapshot#updateTime
let
documentRef
=
firestore
.
doc
(
'col/doc'
);
documentRef
.
get
().
then
(
documentSnapshot
=
>
{
if
(
documentSnapshot
.
exists
)
{
let
updateTime
=
documentSnapshot
.
updateTime
;
console
.
log
(
`Document updated at '
${
updateTime
.
toDate
()
}
'`
);
}
});
Methods
data()
data
()
:
T
|
undefined
;
Retrieves all fields in the document as an object. Returns 'undefined' if the document doesn't exist.
T | undefined
{T|undefined} An object containing all fields in the document or 'undefined' if the document doesn't exist.
let
documentRef
=
firestore
.
doc
(
'col/doc'
);
documentRef
.
get
().
then
(
documentSnapshot
=
>
{
let
data
=
documentSnapshot
.
data
();
console
.
log
(
`Retrieved data:
${
JSON
.
stringify
(
data
)
}
`
);
});
get(field)
get
(
field
:
string
|
FieldPath
)
:
any
;
Retrieves the field specified by field
.
field
any
{*} The data at the specified field location or undefined if no such field exists.
let
documentRef
=
firestore
.
doc
(
'col/doc'
);
documentRef
.
set
({
a
:
{
b
:
'c'
}}).
then
(()
=
>
{
return
documentRef
.
get
();
}).
then
(
documentSnapshot
=
>
{
let
field
=
documentSnapshot
.
get
(
'a.b'
);
console
.
log
(
`Retrieved field value:
${
field
}
`
);
});
isEqual(other)
isEqual
(
other
:
firestore
.
DocumentSnapshot<T>
)
:
boolean
;
Returns true if the document's data and path in this DocumentSnapshot
is equal to the provided value.
other
FirebaseFirestore.DocumentSnapshot
<T>
The value to compare against. {boolean} true if this DocumentSnapshot
is equal to the provided value.
boolean