我正在尝试将15个对象副本推送到Firebase节点,该节点是一个数组。被推送的对象包含questionText和answerText对象。我没有为这个特定部分编写代码,所以我遇到了一些麻烦。
在firebase quizText
中是一个数组。
在commitQuiz方法下方法部分的第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!
}
});
}
}
}
}
答案 0 :(得分:0)
而不是:
quizText: quizText(15).fill(result)
我认为你的意思是:
quizText: Array(15).fill(result)