0
votes

#AskFirebase

When trying to use admin.firestore() part of firebase function execution, I am failing to create new documents, getting the following trace:

Error: Invalid use of type "undefined" as a Firestore argument.
    at Object.exports.customObjectError.val [as customObjectError] (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/validate.js:164:14)
    at Function.encodeValue (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/document.js:808:20)
    at Function.encodeFields (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/document.js:678:36)
    at Function.fromObject (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/document.js:218:55)
    at WriteBatch.set (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/write-batch.js:291:39)
    at DocumentReference.set (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/reference.js:419:8)
    at module.exports.addNewUser.functions.auth.user.onCreate.user (/user_code/functions/auth/index.js:22:73)
    at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27)
    at next (native)
    at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71

enter image description here I followed code samples in https://github.com/firebase/functions-samples and in the official Firestore documentation.

My code snippet:

const functions = require('firebase-functions')
const admin = require('firebase-admin')

admin.initializeApp();

exports.addNewUser = functions.auth
    .user()
    .onCreate(user => {
        console.log(`user=${JSON.stringify(user)}`)

        return admin.firestore().collection(`users`).doc(`${user.uid}`).set({
                name: user.displayName,
                email: user.email
            })
            .then(() => {
                console.log("END")
                return
            })
            .catch((error) => {
                console.log(error)
            });
    })

Failing to perform set() operation here:

admin.firestore().collection(`users`).doc(`${user.uid}`).set({
    name: user.displayName,
    email: user.email
})

with message:

Invalid use of type "undefined" as a Firestore argument:

package.json file

"firebase-admin": "^5.12.0",
"firebase-functions": "^1.0.1",

db rules (no rules):

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write;
    }
  }
}

NOTE: making updates to ref object work well, only when trying to create new docs with full path, I get this behavior

1

1 Answers

0
votes

after cleaning node_modules and updating the firebase cli, seems to work fine