Firebase Functions Issue And The Difference Between .add() Vs. .doc().set()
Solution 1:
with set
you are selecting a document (you assign your own id) and add
you are letting firestore to assign an id for you.
A small (not that small) detail here, your callable functio should return a promise that resolves with the response to send to the client, your function returns a promise that is resolved after an operation against the database.
The line your code is complaining about looks like this:
if (_.isArray(data)) {
return _.map(data, encode);
}
I would put more attention in the data you are sending more than the method you are using.
const saveNewDoc = functions.https.onCall(async (data: NewDocWrite, context: CallableContext) => {
await adminDb.collection(data.collectionPath).add(data.data)
// OR
await adminDb.collection(data.collectionPath).doc().set(data.data)
return Promise.resolve({ ok: true })
})
Actually doc().set()
is equivalent to .add()
, I normally use set
cause I like to have control over the docs Ids I'm setting, but leaving it empty is ok as well. Try that, avoiding returning the promise against the database exec should be good.
Solution 2:
The reason I've found is simple and clear cut:
add()
returns Promise<FirebaseFirestore.DocumentReference>
while
set()
returns `Promise
The operation of adding a new document works in either way but once done, the response is sent back to the client, either the document reference or the write result wrapped in a promise. As there is only valid JSON allowed in the response, returning a document reference will result in an error, because it is not in a valid JSON format.
So once the operation is done, either resolve an empty Promise or transform the result into valid JSON before wrapping it in the response - or use set().
Post a Comment for "Firebase Functions Issue And The Difference Between .add() Vs. .doc().set()"