简体   繁体   English

如何在Firebase Firestore中获取单个文档数据(无承诺)

[英]How to get a single document data in a Firebase Firestore (without promises)

im trying to get the data of a single document and save it to a variable for use it later in another logic operation. 我试图获取单个文档的数据并将其保存到变量中,以供以后在另一个逻辑操作中使用。 The firestore documentation show hot to do this only with promises but i can't set the doc.data() value to a variable for later use, only inside the then() or catch() method. Firestore文档显示仅使用promise即可做到这一点,但我无法将doc.data()值设置为变量以供以后使用,仅在then()或catch()方法内部。 I can set a variable there but i can't use it outside the promise. 我可以在那里设置变量,但是不能在诺言之外使用它。

Firestore documentation example: Firestore文档示例:

var docRef = db.collection("cities").doc("SF");

docRef.get().then(function(doc) {
    if (doc.exists) {
        console.log("Document data:", doc.data());
    } else {
        console.log("No such document!");
    }
}).catch(function(error) {
    console.log("Error getting document:", error);
});

As Jaromanda X commented: you're best of accepting the asynchronous nature of Firebase and most of the modern web. 正如Jaromanda X所说:您最好接受Firebase和大多数现代Web的异步特性。 The sooner you get to grips with the programming patterns, the sooner you can get back to being productive in this paradigm. 您越早掌握编程模式,就越早可以恢复这种范例的生产力。

I find it most helpful to reframe problems. 我发现重新构造问题最有帮助。 Instead of "Load a document, then do something with it" I frame it as "First start loading a document. Once the document has loaded, do something with it". 我将其构架为“首先开始加载文档。加载文档后,对其进行操作”,而不是“加载文档,然后对其进行处理”。

In practice that means that you move your code that needs the document into the completion handler where you now have the logging statements. 实际上,这意味着您将需要文档的代码移动完成处理程序中,在该处理程序中现在有了日志记录语句。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase Firestore 如何在没有额外请求的情况下获取参考数据? - Firebase firestore how to get reference data without additional request? 如何在 firestore 中获取单个文档并将其显示在网站上 - How to get a single document in firestore and display it on a site 如何在Firestore中获取1个文档? - How do I get 1 single document in firestore? firestore firebase 问题:如何在不指定文档名称的情况下更新文档 - firestore firebase problem: how to update the document without specifying the name of the document Firebase Firestore:离线添加数据后获取文档ID - Firebase Firestore: get document ID after adding data offline Firebase firestore onSnapshot - 使用承诺在初始数据查询后获取实时更新 - Firebase firestore onSnapshot - Get real-time updates after initial data query using promises 如何使用 firebase function 从 firestore 检索文档并从另一个 firestore 文档添加新数据? - How to retrieve a document from firestore using firebase function and add new data from another firestore document? 在 Firebase/firestore 中添加项目而不覆盖 Document - Adding items without overwriting Document in Firebase/firestore 在Firebase Firestore中,如何在没有数据的情况下获取所有文档的ID? - How to get the id's for all documents without the data in a parsed collection in Firebase Firestore? 如何将多个承诺链接到Firebase中的单个事务中 - how to chain multiple promises into a single transaction in Firebase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM