gpt4 book ai didi

firebase - 如何从 Firestore 查询快照中获取特定文档数据?

转载 作者:行者123 更新时间:2023-12-04 05:13:27 26 4
gpt4 key购买 nike

我在一个函数中得到了一个查询快照。
并希望将整个查询快照带到另一个函数(functionTwo)。
在函数二中,我想在没有 forEach 的情况下获取查询快照中的特定文档。具体的文档可以根据不同的情况进行更改。

ref_serial_setting.get()
.then(querysnapshot => {
return functionTwo(querysnapshot)
})
.catch(err => {
console.log('Error getting documents', err)
})


let functionTwo = (querysnapshot) => {
// getting value

const dataKey_1 = "dataKey_1"

// Tried 1
const value = querysnapshot.doc(dataKey_1).data()

// Tried 2
const value = querysnapshot.document(dataKey_1).data()

// Tried 3 (Put 'data_name': dataKey_1 in that doc)
const value = querysnapshot.where('data_name', '==', dataKey_1).data()
}

结果是所有这些尝试都不是功能。

如何从查询快照中获取特定的文档数据?

或者

有什么简单的方法可以将查询快照更改为 JSON?

最佳答案

您可以使用 docs 获取文档快照数组。 QuerySnapshot 的属性.之后,您必须循环获取文档快照的数据以查找您的文档。

const docSnapshots = querysnapshot.docs;

for (var i in docSnapshots) {
const doc = docSnapshots[i].data();

// Check for your document data here and break when you find it
}

如果您实际上不需要完整的 QuerySnapshot ,您可以使用 where 应用过滤器功能 之前 调用 get在查询对象上:
const dataKey_1 = "dataKey_1";    
const initialQuery = ref_serial_setting;
const filteredQuery = initialQuery.where('data_name', '==', dataKey_1);

filteredQuery.get()
.then(querySnapshot => {
// If your data is unique in that document collection, you should
// get a query snapshot containing only 1 document snapshot here
})

.catch(error => {
// Catch errors
});

关于firebase - 如何从 Firestore 查询快照中获取特定文档数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50692218/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com