我有一个包含 2 个字段的文档:field1、field2(为简单起见已更改名称)。
我试图从云函数中获取 field1 的值。这些函数不是该特定文档的触发器,我得到的值如下:
const user_collection = db.collection("user")
const photoName = user_collection.doc(userid).field1
但是,我得到的值在 undefined
中返回。我尝试使用 data() 或 get() 方法,但最终出现错误 ... is not a function
。在文档或 SO 中找不到任何帮助我从文档中获取字段值的内容。
关于如何提取该字段有什么建议吗?
编辑:
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.deleteUser = functions.firestore.document('user/{userID}').onDelete((snap, context) => {
const deletedValue = snap.data().photo;
});
来自docs :
You can also trigger a function when a document is deleted using the
onDelete()
function with a wildcard.
在您的代码中,您使用的是 authentication trigger
这就是您无法访问文档中的 photo
字段的原因。
来自docs :
onDelete
Event handler that fires every time a Firebase Authentication user is deleted.
onDelete()
有一个类型为 UserRecord
的参数。您可以在此处查看 UserRecord
的属性:
https://firebase.google.com/docs/reference/functions/functions.auth.UserRecord.html
关于javascript - 无法从云函数中的文档获取数据(返回未定义),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56236806/