【问题标题】:How to return value from firestore database?如何从firestore数据库返回值?
【发布时间】:2019-12-30 21:54:58
【问题描述】:

我很难从 firestore 数据库返回值。 我正在尝试从数据库中返回“金额”。 设置变量时,我可以控制台记录“金额”。 (见代码) 但是当我尝试在函数末尾返回值时,它不会返回任何内容。('amount' 未定义 no-undef) 我怎样才能返回这个值。任何帮助都会很棒。请记住,我对这个话题还是很陌生。

        import firebase from 'firebase/app';
        import 'firebase/firestore';
        import 'firebase/auth';

        export default function checkAmount() {

            let user = firebase.auth().currentUser;

            if (user) {
                let userUID = user.uid
                let docRef = firebase.firestore().collection("users").doc(userUID);

                docRef.get().then((doc) => {
                    if (doc.exists) {
                            let amount = doc.data().amount;
                            if (amount > 0){
                                console.log(amount) /// This does work
                            }
                        } else {
                            console.log("No such document!");
                        }
                    }).catch(function(error) {
                        console.log("Error getting document:", error);
                    });
            }

            return amount /// This **does not** return anything . How do i return the amount?
        }

【问题讨论】:

  • 能否分享一下datastore中的结构?

标签: javascript firebase google-cloud-firestore


【解决方案1】:

原因是因为get() 方法是异步的:它立即返回一个promise,该promise 会在一些 时间后用查询结果解析。 get() 方法不会阻塞函数(它立即返回,如上所述):这就是为什么最后一行 (return amount) 在异步工作完成之前执行但具有未定义的值。

您可以阅读更多关于异步 JavaScript 方法的 here 和关于 Firebase API 异步原因的 here

因此,您需要等待 get() 返回的承诺解析并使用 then() 方法(如 Alex 所述)来接收查询结果并发送响应。

以下将起作用:

    export default function checkAmount() {

        let user = firebase.auth().currentUser;

        if (user) {
            let userUID = user.uid
            let docRef = firebase.firestore().collection("users").doc(userUID);

            return docRef.get().then((doc) => {  //Note the return here
                if (doc.exists) {
                        let amount = doc.data().amount;
                        if (amount > 0){
                            console.log(amount) /// This does work
                            return true;  //Note the return here
                        }
                    } else {
                        console.log("No such document!");
                        //Handle this situation the way you want! E.g. return false or throw an error
                        return false;
                    }
                }).catch(error => {
                    console.log("Error getting document:", error);
                    //Handle this situation the way you want
                });
        } else {
           //Handle this situation the way you want
        }

    }

但您需要注意,您的函数现在也是异步的。因此你应该这样称呼它:

checkAmount().
then(result => {
  //Do whatever you want with the result value
  console.log(result);
})

【讨论】:

  • 谢谢!现在知道该函数是异步的,这确实起到了作用。
猜你喜欢
  • 1970-01-01
  • 2019-11-14
  • 1970-01-01
  • 1970-01-01
  • 2023-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多