2016-12-28 3 views
-1

Ich versuche, 15 Kopien eines Objekts an einen Firebase-Knoten zu senden, bei dem es sich um ein Array handelt. Das Objekt, das verschoben wird, enthält quoteText und ein answerText-Objekt. Ich habe den Code für diesen Teil nicht geschrieben, also habe ich ein paar Probleme.Kopieren von Kopien zu Firebase

In Firebase quizText ist ein Array.

enter image description here

Unten auf der Leitung 125 in dem Abschnitt Verfahren unter der submitQuiz Methode, die ich versuche, 15 Kopien des Ergebnisses in das quizText Array zu erhalten, aber es sagt, dass quizText nicht definiert ist. Was soll ich anders machen?

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! 
       } 
      }); 
     } 
    } 
} 

}

Antwort

0

Statt:

quizText: quizText(15).fill(result) 

Ich glaube, Sie gemeint:

quizText: Array(15).fill(result) 
Verwandte Themen