You have unlimited access as a PRO member
You are receiving a free preview of 3 lessons
Your free preview as expired - please upgrade to PRO
Recent Posts
- Object Oriented Programming With TypeScript
- Angular Elements Advanced Techniques
- TypeScript - the Basics
- The Real State of JavaScript 2018
- Cloud Scheduler for Firebase Functions
- Testing Firestore Security Rules With the Emulator
- How to Use Git and Github
- Infinite Virtual Scroll With the Angular CDK
- Build a Group Chat With Firestore
- Async Await Pro Tips
How to Format Document Data for the Firestore REST API
written by Jeff DelaneyFormatting data for the Firestore REST API is a bit of a pain. A document’s data is not true JSON, so we need to explicitly tell the database the type of every value. This is one of those situations where LoDash is extremely helpful. Not only will we use it to map object values, but we can also use it to check datatypes with isString
, isArray
, etc.
Let’s imagine we have a plain JS object that looks like:
data = { |
The snippet below will convert it to a following Firestore-compliant format that looks like this:.
data = { |
A Recursive Function to Map Firestore REST Requests
We need a way to format every property as a Firestore Value, and that also includes values nested in objects and array. It’s a perfect situation for a recursive function when we hit an ArrayValue or MapValue. This code will work for all JS primitives, but not any of the special Firestore types like GeoPoint
or DocumentReference
.
// Thanks LoDash! |