【发布时间】:2020-05-08 19:24:16
【问题描述】:
我正在使用 Vue.js 创建一个 Web 应用程序(这是我第一次使用它)。该应用程序基本上是一个多用户实时测验,每个用户都必须选择一个角色并回答与他的角色相关的问题。我使用 Cloud Firestore 数据库中的集合来存储与每个角色相关联的问题以及与每个问题相关联的答案。此外,每个答案的特点是一个字段“nextQuestion”包含下一个要可视化的问题的 id,一个字段“nextUser”包含与这个新问题相关的下一个用户的 id(这些字段用于查询选择下一个问题及其可能的答案)和一个布尔字段“默认”,如果为真,则在用户未在设定时间内回答问题的情况下选择的答案(其他字段指示文本答案)。我通过查询获得问题和答案,以便在 webapp 上将它们可视化。 我的问题与用户在设定的时间内没有回答问题的情况有关(同时如果用户在设定的时间内选择了答案,我没有问题)。当回答的时间到期时,我调用这个函数:
CountTerminated: function () {
if(this.optionSelected == false){ //optionSelected is a component variable that informs if a user has selected or not an answer
this.onNotSelectedAnswer(this.getDefaultAnswer())
}
else{
this.onClickButtonConfirm() //function called if the user selects an answer within the set time
}
}
getDefaultAnswer() 函数获取与当前问题关联的默认答案的字段(其中“nextUser”和“nextQuestion”)(通过查询)并通过变量返回:
getDefaultAnswer(){
var data
db.collection("Utenti").doc(this.userId).collection("Domande").doc(this.questionId).collection("Risposte").where("default","==",true).get().then(querySnapshot =>{
querySnapshot.forEach(doc=>{
data = doc.data()
})
})
return data
},
onNotSelectedAnswer(data)函数主要接收getDefaultAnswer()返回的值,将“data”赋值给组件变量answerId,并更新组件变量“userId”的值(告知角色的作用)必须回答当前问题的用户),组件变量“questionId”的值(包含当前问题的 id)和 questionValue 的值(包含当前问题的文本)使用函数 setUserId() , setQuestionId(), setQuestionValue()
onNotSelectedAnswer: function(data){
if(this.userChoice == this.userId){
this.answerId = data
this.setUserId(this.answerId.nextUser)
this.setQuestionId(this.answerId.nextQuestion)
this.setQuestionValue()
this.optionSelected = false
this.answers=[]
this.isActive = ""
this.getAnswers() //function used to query (using updated values of userId and questionId variable) the DB to obtain a question and its related possible answers
var a = this.channel.trigger('client-confirmEvent',{ user: this.userId, question : this.questionId, questionval: this.questionValue})
console.log(a)
}
}
问题与 onNotSelectedAnswer() 函数中的 answerId 是“未定义”而不是包含查询结果以及我将用于上传新问题的数据有关。我不明白是哪个错误,希望您能帮助我。
【问题讨论】:
-
在 getDefaultAnswer() 上,我建议你在使用你的方法之前检查 querySnapshot 变量是否不为空。
标签: javascript vue.js vuejs2 google-cloud-firestore vue-component