firebase-functions: 1.04
firebase-tools: 3.18.5
firebase-admin: 5.12.1
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
exports.userAuth = functions.auth.user().onCreate(userRecord => admin.database().ref(`/USER_RECORD/${userRecord.uid}`).set(userRecord.toJSON()));
deploy given example
create new user
yes
userRecord.toJSON() returns a valid JSON
object returned by userRecord.toJSON() contains functions.
most likely this is caused by:
firebase-functions/lib/providers/auth.js:111
_.forEach(record.providerData, entry => {
_.set(entry, 'toJSON', () => {
return entry;
});
});
_toJSON_ returns _this_ instead of JSON
Stacktrace:
Error: Reference.set failed: First argument contains a function in property '${path}.${userRecord.uid}.providerData.0.toJSON' with contents = () => { return entry; }
at validateFirebaseData (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:1436)
at (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:1479)
at Object.forEach (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/node_modules/@firebase/util/dist/index.node.cjs.js:835)
at validateFirebaseData (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:1462)
at (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:1479)
at Object.forEach (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/node_modules/@firebase/util/dist/index.node.cjs.js:835)
at validateFirebaseData (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:1462)
at (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:1479)
at Object.forEach (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/node_modules/@firebase/util/dist/index.node.cjs.js:835)
at validateFirebaseData (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:1462)
at validateFirebaseDataArg (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:1421)
at Reference.set (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:13963)
at * (/user_code/*.js:12)
at * (/user_code/*.js:34)
at * (*.js:45)
at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112)
This is indeed a bug. However, what if you used userRecord itself instead of userRecord.toJSON()? I believe it is already a valid JSON.
Internal bug reference: 110432691
@timothegenzmer is this issue still happening for you?
I was able to reproduce this - will be adding a fix soon. Thanks!
Still facing the same issue!
Yep. The same. I was trying userRecord.toJSON() - no luck. Later, I decided to write it attribute by attribute. metadata.toJSON() was fine, but providerData: userRecord.providerData.map(item => item.toJSON()), is failing:
Error: Input is not a plain JavaScript object.
But it should work:
From the docs
providerData
non-null Array of non-null admin.auth.UserInfo which should have a toJSON()-method, but it turns out in Error:
Error: Input is not a plain JavaScript object.
at Object.customObjectError (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/build/src/validate.js:162:15)
at Serializer.encodeValue (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/build/src/serializer.js:142:26)
at Serializer.encodeFields (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/build/src/serializer.js:50:34)
at Serializer.encodeValue (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/build/src/serializer.js:135:44)
at Serializer.encodeValue (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/build/src/serializer.js:120:38)
at Serializer.encodeFields (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/build/src/serializer.js:50:34)
at Function.fromObject (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/build/src/document.js:96:53)
at WriteBatch.set (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/build/src/write-batch.js:238:54)
at DocumentReference.set (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/build/src/reference.js:340:27)
at createProfile (/user_code/auth/createUserProfile.js:17:6)
try JSON.parse(JSON.stringify(record));
Hi @pariola and @Carduelis thanks for chiming in - this issue slipped through the cracks, but we're still planning to put in a fix. In the meantime, please use the workaround mentioned in https://github.com/firebase/firebase-functions/issues/270#issuecomment-457759775
Most helpful comment
Hi @pariola and @Carduelis thanks for chiming in - this issue slipped through the cracks, but we're still planning to put in a fix. In the meantime, please use the workaround mentioned in https://github.com/firebase/firebase-functions/issues/270#issuecomment-457759775