'Firebase functions not triggered to populate Firestore collection, even when the deployment is successful. Why?
I want a new doc to be added to my user collection which is triggered by firebase.auth() but the db is not getting populated.
This is my firebase function:
exports.newUser = functions.auth.user().onCreate((user) => {
return db
.collection("user")
.doc(user.uid)
.create(JSON.parse(JSON.stringify(user)));
});
This is my registeration method:
export const register = (email, password) => dispatch => new Promise((resolve, reject) => {
firebase.auth().createUserWithEmailAndPassword(email, password)
.then(() => {
resolve()
})
.catch(() => {
console.log(typeof (email, password))
reject()
})
})
Upon execution the new user is added to Authentication but doesn't trigger the new document creation. The function newUser is successfully deployed to firebase.
Seems like my method of parsing user data is incorrect, please suggest what am I missing?
Function logs:
4:38:51.935 PM outlined_flag
newUser Function execution started 4:38:52.968 PM newUser TypeError: entry.toJSON is not a function 4:38:52.968 PM newUser at /workspace/node_modules/firebase-functions/lib/common/providers/identity.js:113:70 4:38:52.968 PM newUser at Array.map () 4:38:52.968 PM newUser at Object.record.toJSON (/workspace/node_modules/firebase-functions/lib/common/providers/identity.js:113:49) 4:38:52.968 PM newUser at JSON.stringify () 4:38:52.968 PM newUser at /workspace/index.js:10:33 4:38:52.968 PM newUser at cloudFunction (/workspace/node_modules/firebase-functions/lib/cloud-functions.js:135:23) 4:38:52.968 PM newUser at /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/function_wrappers.js:144:25 4:38:52.968 PM newUser at processTicksAndRejections (node:internal/process/task_queues:96:5) 4:38:52.968 PM newUser TypeError: entry.toJSON is not a function 4:38:52.968 PM newUser at /workspace/node_modules/firebase-functions/lib/common/providers/identity.js:113:70 4:38:52.968 PM newUser at Array.map () 4:38:52.968 PM newUser at Object.record.toJSON (/workspace/node_modules/firebase-functions/lib/common/providers/identity.js:113:49) 4:38:52.968 PM newUser at JSON.stringify () more_vert 4:38:52.968 PM newUser at /workspace/index.js:10:33 4:38:52.968 PM newUser at cloudFunction (/workspace/node_modules/firebase-functions/lib/cloud-functions.js:135:23) 4:38:52.968 PM newUser at /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/function_wrappers.js:144:25 4:38:52.968 PM newUser at processTicksAndRejections (node:internal/process/task_queues:96:5) 4:38:52.979 PM outlined_flag
newUser Function execution took 1044 ms. Finished with status: error
Solution 1:[1]
firebaser here
From what I can tell this bug was introduced into our Functions SDK recently. You can check its status on the Github repo issue #1102.
Solution 2:[2]
UPDATE:
I tried this function and it does the job:
exports.newUser = functions.auth.user().onCreate((user) => {
return db
.collection("user")
.doc(user.uid)
.set({
email: user.email,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString()
})
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Frank van Puffelen |
| Solution 2 | Anupam Arya |
