Firebase Firestore管理员无法在Firebase功能环境

时间:2018-04-21 10:52:06

标签: firebase firebase-authentication google-cloud-firestore google-cloud-functions firebase-admin

#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

enter image description here 我在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",

db rules(无规则):

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

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

1 个答案:

答案 0 :(得分:0)

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