Reading Properties in VueJS with Firebase

<template>
<div id="app">
<h1 id="title"gt;{{ quiz.title }}</h1>
  <div id="ques" v-for="(question, index) in quiz.questions" :key="question.text">
    <div v-show="index === questionIndex">
    <div id="tFlex">
      <p id="indexStuff"gt;{{index+1}}</p>
      <h2 id="quest"gt;{{ quiz.question.text }}</h2>
    </div>
      <ol>
        <li id="choices"> v-for="response in question.responses" :key="response.text">
          <label>
            <input id="responce" type="radio" v-bind:value="response.correct" v-bind:name="index" v-model="userResponses[index]">
            <span class="t">{{response.text}}</span>
          </label>
        </li>
      </ol>
      <div id="btns">
        <button id="prevbtn"> v-if="questionIndex > 0" v-on:click="prev"<previous Question></button>
        <button id="nxtbtn"> v-on:click=<Next Question></button>
      </div>
    </div>
  </div>
  <div v-show="questionIndex === quiz.questions.length">
    <h2 id="fint"gt;Quiz finished</h2>
    <p id="sct">Total score: {{ score() }} / {{ quiz.questions.length }}</p>
    <div id="btnFlex"> 
      <button id="chck"v-show="questionIndex === quiz.NumQuestions" v-on:click="check">Check Answers</button>
    </div>
  </div>
</div>
</template>

<script>
import { db } from './firebase';

const DefaultPath = 'quiz/01';

var Quiz;

db.doc(DefaultPath).get().then((doc) => {
  Quiz = doc.data();
});

const quiz = Quiz;

console.log(quiz);

export default {
  data() {
    return{
      quiz:quiz,
      questionIndex: 0,
      canDo: true,
      userResponses:Array(quiz.questions.length).fill(false),
    }
  },
  methods: {
  next: function() {
    this.questionIndex++;
  },
  prev: function() {
    this.questionIndex--;
  },
  score: function() {
    return this.userResponses.filter(function(val) { return val }).length;
  },
  check: function() {
      this.questionIndex = 0;
      this.canDo = false;
      this.$forceUpdate();
  },
  cando: function() {
    return {canDo:!this.canDo};
  },
}, 
}
</script>

<style>
@import './style.css';
</style>

I encountered an unusual bug with my Vue.js project. As a newcomer to Vue.js and web firebase technologies, I'm grappling with resolving the issue.

Upon inspecting the Chrome developer tools, I came across an error while attempting to create a quiz application integrated with Firebase. Initially, I experimented with Firestore procedures before delving into the data function intricacies. My journey in the realm of vuejs is just beginning.

https://i.stack.imgur.com/NxUa4.png

Answer №1

It appears that there may be some issues with storing data in the quiz variable. However, here is a way to retrieve a collection and store it:

<script>

  import firebase from 'firebase'
  export default {
    data: function() {
      return {
        quiz: [],
      }
    },

    firestore() {
      return {
        quiz: firebase.collection('Collection name here')
      }
    },
   
  }
</script>

If you need to access a specific document within a collection, you can do so using the following code snippet:

<script>

  import firebase from 'firebase'
  export default {
    data: function() {
      return {
        quiz: [],
      }
    },

    firestore() {
      return {
        quiz: firebase.collection('Collection name here').doc('doc id here')
      }
    },
   
  }
</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to iterate through my array and display each value within my button element?

I have a challenge where I'm trying to iterate over an array named topics. This array contains the names of various people. Within my loop, my goal is to extract each name and insert it into a button as text. When I use console.log within my loop, I ...

Issue arises when annotation is utilized in ChartJs, as the tooltip fails to appear

Upon hovering over a dot, only a blue line is displayed without showing a tooltip as expected below: Library Version: "chart.js": "^2.9.3", "chartjs-plugin-annotation": "^0.5.7" Actual : enter image descriptio ...

Customize Nuxt default document using a custom app.html file

According to the guidelines provided by Nuxt documentation, you have the ability to customize the default document by... placing an app.html file in the source directory of your project, which is typically the root directory. I followed these instructio ...

Prop type failure: The `actions` prop is specified as mandatory in the `Testing` component, however, its value is currently undefined

I am working on a project that involves creating a login form using React and Redux. Here's a snippet of my app.js: import React from 'react'; import { render } from 'react-dom'; import Input from 'react-toolbox/lib/input&apo ...

Using React - What is the best way to invoke a function from within a different function?

Imagine this scenario: class Navigation extends React.Component { primaryFun() { console.log('funn') } secondaryFun() { this.primaryFun(); } } I assumed that calling primaryFun within secondaryFun would work as expected, but instead I rec ...

Looking to transform a list into JSON format using JavaScript?

I have a collection that looks like this: <ol class="BasketballPlayers"> <li id="1">Player: LeBron, TotalPoints: 28753, MVP: true</li> <li id="2">Player: Steph, TotalPoints: 17670, MVP: true< ...

Replace the default focus state using CSS or resetting it to a custom style

I'm looking for a solution similar to a CSS reset, but specifically for the :focus state. If such a thing doesn't exist yet, I'm interested in learning about the possible properties that can be reset or overridden in order to create a new :f ...

Contrasting the disparities between creating a new RegExp object using the RegExp constructor function and testing a regular

Trying to create a robust password rule for JavaScript using regex led to some unexpected results. Initially, the following approach worked well: const value = 'TTest90()'; const firstApproach = /^(?=(.*[a-z]){3,})(?=(.*[A-Z]){2,})(?=(.*[0-9]){2 ...

Issue with CSS background scaling bug

Is it possible to make the cloud background scale within the box? The current issue can be viewed here: I have tried various methods to resolve the problem where the background scales even beyond the limited box. However, I could not identify the solutio ...

React: "The function is not toISOString"

I am currently working on a Todo List project using Next.js. As part of the functionality, I am trying to implement a due date feature. However, when I make a post request, I encounter the following error: Unhandled Runtime Error TypeError: dueDate.toISOSt ...

Displaying photos locally in HTML is not supported

I've encountered an issue with my HTML code. I'm trying to load images from my local drive, and the path to the folder seems correct because I can open the images by ctrl + clicking on them. However, when I open my page using a live server or hos ...

Strategies for handling uncaught promise rejections within a Promise catch block

I'm facing a challenge with handling errors in Promise functions that use reject. I want to catch these errors in the catch block of the Promise.all() call, but it results in an "Unhandled promise rejection" error. function errorFunc() { return ne ...

Internet Explorer 10 not triggering the 'input' event when selecting an option from the datalist

Within this particular scenario, there is an input field paired with a corresponding datalist element. My aim is to develop JavaScript code that actively listens for when a user chooses an item from the list. Most resources suggest utilizing the "input" ev ...

Error code E11000 is thrown due to a duplicate key in a Node.js application

Whenever I input data on the webpage, it syncs correctly with the database. However, when I attempt to fill out the same form again, an error occurs: { "code": 11000, "index": 0, "errmsg": "E11000 duplicate key error collection: test.creates i ...

Having trouble getting $compile to work in an injected cshtml file with Angular

Summary I am currently working on a large application where everything is designed to be very generic for easy expansion. One of the components I am focusing on is the dialog. Before suggesting alternatives like using ngInclude or angular templates, let m ...

What could be causing my textarea-filling function to only be successful on the initial attempt?

Programming: function updateText() { $("body").on("click", ".update_text", function(event) { $(this).closest("form").find("textarea").text("updated text"); event.preventDefault(); }); } $(document).ready(function() { updateText(); }); H ...

Indicator malfunctioning on Carousel feature

I created a carousel containing six photos, but I am encountering an issue with the carousel indicators below. The first three indicators work correctly – when clicked, they take me to the corresponding slide (e.g., clicking the 1st indicator takes me ...

Guide on removing a row from el-table using Vue.js

Below is the code for my el-table: <div class="row"> <div class="col-sm-12"> <el-table :data="membersList"> <el-table-column label="Name" prop="member_name" align="cent ...

Display PDF in Forge Viewer using PDF Extension - warning generated by pdf.worker.js

Whenever we attempt to display a PDF file using our own API, the pdf.worker.js generates a warning message and the PDF always appears completely white. https://i.stack.imgur.com/IqGML.png All I can see is this (it's a wide PDF that renders fine in t ...

Using Google Script Code in Sheet to input a key and click on the submission button

Is there a way to enable using the Enter key in addition to clicking the submit button to input data and save it to a cell? I'm having trouble getting my current code to work. Any suggestions on how to modify it? <script> var itemBox = document ...