Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法从云函数中的文档获取数据(返回未定义)_Javascript_Firebase_Google Cloud Firestore_Google Cloud Functions - Fatal编程技术网

Javascript 无法从云函数中的文档获取数据(返回未定义)

Javascript 无法从云函数中的文档获取数据(返回未定义),javascript,firebase,google-cloud-firestore,google-cloud-functions,Javascript,Firebase,Google Cloud Firestore,Google Cloud Functions,我有一个包含两个字段的文档:field1,field2(为了简单起见更改了名称) 从一个云函数中,我试图从field1中获取值。函数不是特定文档的触发器,我得到如下值: const user_collection = db.collection("user") const photoName = user_collection.doc(userid).field1 但是,我在未定义中得到的值。我尝试使用data()或get()方法,但最终出现错误…不是函数。在文档中找不到帮助我从文档中获取字段

我有一个包含两个字段的文档:field1,field2(为了简单起见更改了名称)

从一个云函数中,我试图从field1中获取值。函数不是特定文档的触发器,我得到如下值:

const user_collection = db.collection("user")
const photoName = user_collection.doc(userid).field1
但是,我在
未定义中得到的值。我尝试使用data()或get()方法,但最终出现错误
…不是函数。在文档中找不到帮助我从文档中获取字段值的任何内容

关于如何提取该字段有什么建议吗

编辑:

exports.onUserDeletion = functions.auth.user().onDelete((user) => {

    const userid = user.uid
    const photoName = user_collection.doc(userid).photo    //<--- this is "undefined"
    const filePath = `user_photo/${photoName}`
    const file = bucket.file(filePath)

    console.log(`userid: ${userid} photoName: ${photoName} filePath: ${filePath} file: ${file}`)

    return highscore_collection.doc(userid).delete().then(user => {
        return user_collection.doc(userid).delete().then(user => {
            return file.delete()
        })
    })

})
exports.onUserDelete=functions.auth.user().onDelete((用户)=>{
const userid=user.uid
const photoName=user\u collection.doc(userid).photo//{
返回user_collection.doc(userid).delete()。然后(user=>{
返回文件。删除()
})
})
})

您应该执行以下操作:

exports.deleteUser = functions.firestore.document('user/{userID}').onDelete((snap, context) => {
        const deletedValue = snap.data().photo;
});
从:

使用带有通配符的
onDelete()
函数删除文档时,也可以触发函数


在代码中,您使用的是一个字段,这就是为什么您无法访问文档中的
photo
字段的原因

从:

onDelete

每次删除Firebase身份验证用户时激发的事件处理程序

onDelete()
具有类型为
UserRecord
的参数。您可以在此处检查
UserRecord
的属性:


用户已被删除。这就是为什么从数据库中获取它不起作用

exports.onUserDeletion = functions.auth.user().onDelete((user) => {   
        const userid = user.uid
        const photoName = user.photo    //<--- this is "undefined"
        const filePath = `user_photo/${photoName}`
        const file = bucket.file(filePath)

        console.log(`userid: ${userid} photoName: ${photoName} filePath: ${filePath} file: ${file}`)

        return highscore_collection.doc(userid).delete().then(user => {
            return user_collection.doc(userid).delete().then(user => {
                return file.delete()
            })
        })

    })
exports.onUserDelete=functions.auth.user().onDelete((用户)=>{
const userid=user.uid
const photoName=user.photo//{
返回user_collection.doc(userid).delete()。然后(user=>{
返回文件。删除()
})
})
})

添加您的云功能和数据库的屏幕截图阅读文档应该会有所帮助:@PeterHaddad我编辑了我的问题以添加这些功能。您如何知道其未定义?控制台。日志?@SomeshMukherjee是的,没错。我也用console.log()部分编辑了我的问题的整个函数。