简体   繁体   English

在 Vue.JS 中获取 firebase firestore 后如何生成数据

[英]How to produce data after getting firebase firestore in Vue.JS

I want to show all of the orders data that I get from firebase to my browser console.我想显示从 firebase 到浏览器控制台的所有订单数据。 Please look at the picture below请看下面的图片

在此处输入图片说明

this is the data which get from firebase after written some codes like below in cue.js这是在cue.js中编写如下代码后从firebase获取的数据

orders(){
    const db = firebase.firestore();
    const dbOrderRef = db.collection('order');
    dbOrderRef.get()
        .then(res => {
            res.forEach(doc => {
                console.log(doc.data())
            })
        })
        .catch(err => {
            console.log('error', err)
        })
}

I have tried to like this below我试图在下面喜欢这个

The instance variable in data数据中的实例变量

ordersData = []

then in method然后在方法中

this.ordersData = doc.data()

but no luck.但没有运气。

What can I do for now to achieve my goal?我现在可以做什么来实现我的目标?

Thanks谢谢

If I understand correctly your question, you want to assign to an ordersData Array all the orders received from a Firestore query.如果我正确理解您的问题,您希望将从 Firestore 查询收到的所有订单分配给ordersData数组。

The following should do the trick:以下应该可以解决问题:

const db = firebase.firestore();
const dbOrderRef = db.collection('order');

let orderArray = [];
dbOrderRef.get()
    .then(res => {
        res.forEach(doc => {
            let orderObj = doc.data();
            orderObj.id = doc.id;
            orderArray.push(orderObj);
        });
        this.ordersData = orderArray;
    })
    .catch(err => {
        console.log('error', err)
    })

Then you can display the ordersData array in your template as follows, for example:然后您可以在模板中显示ordersData数组,如下所示,例如:

            <div v-if="ordersData.length">
                <div v-for="order in ordersData">
                    <h5>{{ order.name }}</h5>
                    <p>Price: {{ order.price }}</p>
                    <p>
                     <a @click="openOrderDetail(order.id)">Open order</a>  
                     // Here we show how we can call an openOrderDetail() method passing the Firestore document id of the order.
                     // This would allow, for example to route to a page where you use the order id to query for the order document.
                    </p>
                </div>
            </div>

While working with then , this keyword used inside the then block refers to the block itself and not the Vue object.在使用then ,在 then 块中使用的这个关键字指的是块本身而不是 Vue 对象。 The following solution should work as the orders function, in which we save the Vue reference in another variable and use this variable to access ordersData.下面的解决方案应该作为 orders 函数工作,我们将 Vue 引用保存在另一个变量中,并使用该变量访问 ordersData。

let _this = this
const db = firebase.firestore();
const dbOrderRef = db.collection('order');
dbOrderRef.get()
    .then(res => {
        res.forEach(doc => {
            _this.ordersData = doc.data();
            console.log(_this.ordersData)
        })
    })
    .catch(err => {
        console.log('error', err)
    })

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM