Firebase Firestore 管理员在 Firebase Function 环境中不起作用

Firebase Firestore admin doesn't work in Firebase Function environment

#AskFirebase

当尝试使用 admin.firestore() 部分 firebase 函数执行时,我无法创建新文档,得到以下跟踪:

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

我遵循 https://github.com/firebase/functions-samples 中的代码示例 在官方 Firestore 文档中。

我的代码片段:

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)
            });
    })

此处执行 set() 操作失败:

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

留言:

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

package.json 文件

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

数据库规则(无规则):

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

注意:使 ref 对象的更新工作正常,只有在尝试创建具有完整路径的新文档时,我才会出现此行为

清理 node_modules 并更新 firebase cli 后,似乎工作正常