【问题标题】:Pushing copies to Firebase将副本推送到 Firebase
【发布时间】:2017-05-13 01:13:25
【问题描述】:

我正在尝试将一个对象的 15 个副本推送到一个 Firebase 节点,该节点是一个数组。被推送的对象包含 questionText 和 answerText 对象。我没有为这个特定部分编写代码,所以我遇到了一些麻烦。

在 firebase 中 quizText 是一个数组。

在 submitQuiz 方法下的方法部分的第 125 行下方,我尝试将 15 个结果副本放入 quizText 数组,但它表示 quizText 未定义。我应该做些什么不同的事情?

import db from '../db';

import ResourceCard from '../components/ResourceCard'
import FlashCard from '../components/FlashCard' // display info after submitting the answers
import Question from '../components/Question'

import { mapMutations, mapGetters, mapState, mapActions } from 'vuex'

export default {
    name: 'quiz',
    components: { 
        ResourceCard, 
        FlashCard,
        Question
    },
firebase() {
    return {
        resource: {
            source: db.ref('resources/' + this.$route.params.resourceId),
            asObject: true
        },
    };
},
data() {
    this.$store.commit('resetForm');

    return {
        options: {
            lightResource: true 
        },
        showLearn: true,
        resourceId: this.$route.params.resourceId,
        passedResources: [],
        answeredQuestionsRes: [],
        resourceLink: window.location.href
    };
},
created() {
    let passedRes = this.$firebaseRefs['passedResources'] = db.ref('/users/' + this.$store.state.userInfo.uid + '/passedResources');
    let answeredQuestionsRes = this.$firebaseRefs['answeredQuestions'] = db.ref('/users/' + this.$parent.$store.state.userInfo.uid + '/answeredQuestions');

    this.$bindAsArray('passedResources', passedRes);
    this.$bindAsArray('answeredQuestions', answeredQuestionsRes);

    // Swap out buttons
    this.showLearn = false;
},
computed: {
    ...mapState({
        answers: state => state.quiz.answeredQuestions,
        submitted: state => state.quiz.submittedStatus,
        selectedCount: state => state.quiz.result.selectedCount,
        result: state => state.quiz.result
    }),
    score() {
        if ( this.resource.quiz === undefined ) return; 

        let isAnswer = (option) => option.isAnswer===true;
        let totalCorrectAnswers = 0;
        this.resource.quiz.forEach((question) => {
            totalCorrectAnswers += question.options.filter(isAnswer).length;
        });
        let selected = this.selectedCount;
        let incorrectCount = (selected > totalCorrectAnswers) ? selected - totalCorrectAnswers: 0; 
        let correctCount = this.result.correctIds.length;

        let amount = correctCount - incorrectCount;

        if (amount < 0) { 
            amount = 0;
        }

        return {
            amount,
            total: totalCorrectAnswers // total correct answer count - used to calculated messages
        };
    }
},
methods: {
    submitQuiz() {

        this.$store.commit('displayAnswers');

        let getCorrectAnswerText = () => {
            let result = {}
            this.result.correctIds.forEach(({quizIndex, index}) => {
                let quiz = this.resource.quiz[quizIndex];
                result[quizIndex] = result[quizIndex] || {}; // default to empty obj.

                // Trying to make copies here. 

                Object.assign(result[quizIndex], {
                    questionText: quiz.text,
                    summaryText: quiz.summaryText,
                    [index]: {
                        text: quiz.options[index].text
                    }
                });
            });

            return {
                [this.resource['.key']]: {
                    quizText: quizText(15).fill(result)
                }
            };
        };

        if (!this.options.testMode) {
            this.$nextTick(function () {
                // delay to next tick, so we have latest computed values
                console.log(this.score);
                if (this.score.amount == this.score.total) {
                    // 100% answer
                    // Push specific resource object to Firebase under `/users/ ` + userInfo.uid + ` /passedResources` node ONCE score reaches 100% on submit. 
                    // console.log('100% answer', this.$store.state.userInfo.uid)
                    let passedRes;
                    // save answered quiz in passedResources
                    // update times passed if all questions answered or score = 100%
                    // console.log('incPassedResource', this.resource);
                    this.$store.commit('incPassedResource', this.resource); // update store
                    // console.log('after mutation', this.$store.state.passedResources);
                    passedRes = this.$store.state.passedResources;
                    this.$firebaseRefs.passedResources.set(passedRes);


                    // save answeredQuestion as text in user/uid/answeredQuestion/resourceID
                    let answerData = getCorrectAnswerText();
                    // console.log('answeredQuestions', answerData);
                    this.$firebaseRefs.answeredQuestions.set(answerData);
                    // no need to update store --> will be loaded into state in study component

                    // --> all questions answered - increment timesPassed on resource
                    let totalTimesPassed = parseInt(this.resource.timesPassed);
                    totalTimesPassed++;
                    // not working yet --> need to load timesPassed of resource, inc. & save back. or maybe use increment of firebase
                    this.$firebaseRefs.resource.child('timesPassed').set(totalTimesPassed); // todo firebase inc. would be handy here!
                }
            });
        }
    }
}

}

【问题讨论】:

    标签: javascript firebase vue.js


    【解决方案1】:

    代替:

    quizText: quizText(15).fill(result)
    

    我想你的意思是:

    quizText: Array(15).fill(result)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-06
      • 2013-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多