0

What im doing wrong to retrieving data from firestore and apply it as my array? Guess I should atleast see objects in console. Should method be somewhere called? My method created() is working, but only if i handle event by @click - then my array is updated(added from database) and also shows in console. Ive read documentation from firebase and result is same. I just started vue.js a couple days ago. Here is my code:

<template>
    <div class="index container">
        <div class="card" v-for="smoothie in smoothies" :key="smoothie.id">
            <div class="card-content">
                <i class="material-icons delete" @click="deleteSmoothie(smoothie.id)">delete</i>
                <!--                                <i class="material-icons delete" @click="created()">delete</i>-->
                <h2 class="indigo-text">{{smoothie.title}}</h2>
                <ul class="ingredients">
                    <li v-for="(ing, index) in smoothie.ingredients" :key="index">
                        <span class="chip">{{ing}}</span>
                    </li>
                </ul>
            </div>
        </div>
    </div>
</template>

<script>
    import db from '@/firebase/init.js'

    export default {
        name: 'Index',
        data() {
            return {
                smoothies: [
                    // {title: 'Banana shake', slug: 'banana-shake', ingredients: ['banans', 'granats'], id: 1},
                    // {title: 'Morning brew', slug: 'morning-brew', ingredients: ['banans', 'orange'], id: 2},
                ]
            }
        },
        methods: {
            deleteSmoothie(id) {
                this.smoothies = this.smoothies.filter(smoothie => {
                    return smoothie.id != id
                })
            },
            created() {
                db.collection('smoothies').get()
                    .then(snapshot => {
                        snapshot.forEach(doc => {
                            console.log(doc.data())
                            let smoothie = doc.data()
                            smoothie.id = doc.id
                            this.smoothies.push(smoothie)

                        })
                    })
            }
        }
    }
</script>

firebase

import * as firebase from "firebase/app";

import "firebase/analytics";

import "firebase/auth";
import "firebase/firestore";


const firebaseConfig = {
    ...
};

const firebaseApp = firebase.initializeApp(firebaseConfig)

export default firebaseApp.firestore()

2

1 Answer 1

2

You put created() in the wrong place. Lifecycle hooks should go in the root options object. methods are basically just functions as properties.

    export default {
        name: 'Index',
        data() {
            return {
                smoothies: [
                    // {title: 'Banana shake', slug: 'banana-shake', ingredients: ['banans', 'granats'], id: 1},
                    // {title: 'Morning brew', slug: 'morning-brew', ingredients: ['banans', 'orange'], id: 2},
                ]
            }
        },
        created() {
                db.collection('smoothies').get()
                    .then(snapshot => {
                        snapshot.forEach(doc => {
                            console.log(doc.data())
                            let smoothie = doc.data()
                            smoothie.id = doc.id
                            this.smoothies.push(smoothie)

                        })
                    })
        },
        methods: {
            deleteSmoothie(id) {
                this.smoothies = this.smoothies.filter(smoothie => {
                    return smoothie.id != id
                })
            }
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.