简体   繁体   English

Vue.js 将动态 v-for 值发布到数据库 (Firebase)

[英]Vue.js post dynamic v-for value to database (Firebase)

facing major issues in processing a dynamic value from a v-for loop into my database.在处理从 v-for 循环到我的数据库的动态值时面临重大问题。 In my Vue app, I am displaying questions in a v-for loop and the users can answer to each question through a form.在我的 Vue 应用程序中,我在 v-for 循环中显示问题,用户可以通过表单回答每个问题。 Additionally to the user input, I want to pass the value of the question they answered (current question object) to my database (Google Firebase).除了用户输入之外,我还想将他们回答的问题(当前问题对象)的值传递给我的数据库(Google Firebase)。

V-for loop + form looks like this: : V-for 循环 + 形式如下所示:

<b-row
    v-for="question in questions"
    :key="question.content"
  >

 <form @submit.prevent="postAnswer">
          <textarea v-model="content"></textarea>
          <input v-model="authorname" />
          <b-form-select v-model="authorage" value="select" name="age">
            <b-form-select-option v-for="n in 99" :value="n" :key="n">{{
              n
            }}</b-form-select-option>
          </b-form-select>
          <b-button type="submit">Submit your answer </b-button>
        </form>

The form is displayed under each question and the user has the possibility to answer the question.表格显示在每个问题下方,用户可以回答问题。

Data objects:数据对象:

data() {
return {
  authorname: null,
  authorage: null,
  content: null,
  question: null,
  questions:[],
  answers: [],
};

}, },

The data is added into Firebase through:通过以下方式将数据添加到 Firebase 中:

postAnswer(){
  db.collection('answers').add({
    authorname: this.authorname,
    authorage: this.authorage,
    content: this.content,
    question: this.question
  })
  .then(docRef => this.$router.go(  ))
  .catch(error => console.log(err))
},

Now as you can see, I do not only want to pass the user's input to my collection on the db but also the question they refer to in the v-for loop.现在如您所见,我不仅想将用户的输入传递给我在数据库上的集合,而且还想将他们在 v-for 循环中引用的问题传递给我。 How can I achieve this?我怎样才能做到这一点?

Thanks a lot非常感谢

I would change questions variable, v-for and v-models to work like the following and add an index variable to postAnswer method.我将更改 questions 变量、v-for 和 v-models 以如下方式工作,并将索引变量添加到 postAnswer 方法。

<b-row
    v-for="(question, index) in questions"
    :key="index">
 <form @submit.prevent="postAnswer">
          <textarea v-model="question.answer.content"></textarea>
          <input v-model="question.answer.authorname" />
          <b-form-select v-model="question.answer.authorage" value="select" name="age">
            <b-form-select-option v-for="n in 99" :value="n" :key="n">{{
              n
            }}</b-form-select-option>
          </b-form-select>
          <b-button @click="postAnswer(index)" type="submit">Submit your answer </b-button>
        </form>
</b-row>

Data would look like:数据看起来像:

data(){
   return{
    questions:[] 
   }
}

When assembling the questions list make sure to add the answer key, something like:组装问题列表时,请确保添加答案键,例如:

this.questions = questions.map(q => {q.answer = {
     'authorname': '',
     'authorage': '',
     'content': ''
}})

Finally the postAnswer something like:最后 postAnswer 类似:

postAnswer(index){
  db.collection('answers').add({
    ...this.questions[index].answer,
    // I didn't understand what question was, but you can just specify here
    question: this.questions[index]
  })
  .then(docRef => this.$router.go(  ))
  .catch(error => console.log(err))
},

This way should work fine, tell me if I missed something这种方式应该可以正常工作,如果我错过了什么,请告诉我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM