Skip to content

get

The method allows one to read a document by its id. It’s available on Collection, Ref, and Doc.

The method returns Doc instance or null if the document doesn’t exist:

const userRef = await db.users.get(userId);
//=> null | Doc<User>
// Update the user
await userRef?.update({ name: "Alexander" });

Subscription

Instead of awaiting the promise returned from get, you can call on on it to subscribe to the document updates:

db.users.get(userId).on((user) => {
//=> null | Doc<User>
});

To stop listening to the updates, call the off function returned from the method:

const off = db.users.get(userId).on((user) => {
// ...
});
// Unsubscribe after 5 seconds
setTimeout(off, 5000);

To catch errors, use catch after calling on:

db.users
.get(userId)
.on((user) => {
// ...
})
.catch((error) => {
//=> PERMISSION_DENIED: Missing or insufficient permissions
});

Read more about subscribing to real-time updates

Options

as

You can tell Typesaurus that it’s safe to use dates by passing the as option ("server" | "client"):

const serverUser = await db.users.get(userId, { as: "server" });
serverUser && serverUser.data.createdAt;
//=> Date
const clientUser = await db.users.get(userId, { as: "client" });
clientUser && clientUser.data.createdAt;
//=> Date | null

By default, Typesaurus uses the "client" option.

Read more about server dates.