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

Node.js used to create animated gif with unique background image

As I navigate my way through the world of node.js and tools like express and canvas, I acknowledge that there might be errors in my code and it may take me some time to grasp certain concepts or solutions. My current project involves customizing a variati ...

Unraveling JSON data retrieved from a MySQL query

After successfully encoding a MySQL result from PHP into JSON, I am now faced with the task of decoding it using JavaScript. Let's assume that my string returned is: [{"0":"x","1":"z"},{"0":"xs","1":"zz"}] I would appreciate some guidance on how to ...

What causes images to unexpectedly expand to fill the entire screen upon switching routes in Next.js?

I'm in the process of creating a website using Next and Typescript, with some interesting packages incorporated: Framer-motion for smooth page transitions Gsap for easy animations One issue I encountered was when setting images like this: <Link&g ...

What is the best way to stop event bubbling in react-router-dom when using the <Link> component?

Hey there! I have a situation that I need help with. I tried putting the Link around the whole post so that I could prevent bubbling for individual buttons, but for some reason stopPropagation() is not working as intended. Following advice from another s ...

Encountered an error when attempting to use the 'removeChild' function on a node that is not a child of the specified node. This issue arose while interacting with an input box

I recently created a todo app with a delete functionality. However, after deleting an element successfully, I encountered an error when typing in the input box stating: Failed to execute 'removeChild' on 'Node': The node to be removed i ...

Node.js application - varying NODE_ENV upon NPM launch

Operating my node.js application can be quite confusing. When launched by npm start, it operates in "production" mode, whereas when launched using node start.js, it runs in 'development' mode. I want to ensure that the 'development' mo ...

Increase the current time by 50 minutes

I have a dropdown menu with various time options like this: <select name="" id="delyvery-hour"> <option value="18:00">18:00</option> <option value="18:05">18:05</option> <option ...

What is the easiest way to connect to MongoDB and add a new post with just a few lines of code?

Currently, I'm working with express.js and node.js. Can you share a brief snippet of code for connecting to mongo and inserting a post into the database? ...

A guide on using key arrows in JavaScript to navigate and focus on elements

When working on my HTML project, I have a list of elements that I want to be able to select using the arrow keys. I've included my code here for reference: [http://jsfiddle.net/T8S7c/] What I'm trying to achieve is when the arrow keys are press ...

Is it possible to enclose an image with two <div> tags in a single line?

Can anyone help me with this layout issue I am facing? I have two <div>s wrapping an image in the same row, but for some reason, the right <div> keeps dropping below. I've attempted using float, display:inline-block, and tweaking the styl ...

React Native encounters issues with removing the reference to the callback attribute upon unmounting

Utilizing a component that I place in an array (of various sizes) and controlling individual components through refs, while adding refs to an object to keep track of each separately. constructor(props){ super(props); this.stamps = []; this.get ...

Having trouble styling my Aside element correctly

I'm having trouble aligning my aside element on the page. It's not lining up properly with the rest of the content and is overlapping into the header area. How can I resolve this issue? Here's a snapshot of what it currently looks like: ht ...

What is the jQuery plugin that can search for elements and display them in real-time within a DIV?

Currently, I am in search of a jQuery plugin that can assist me with the following: Ability to have an input field for user search text Search through my elements and display only those that match the search string entered by the user. Essentially creati ...

Creating a dynamic slider using jQuery

Here is the code snippet I have been working on: var current_slide = 1, animation_time = 1000, pause = 3000, slide_container = $('.slide_container'), interval, height = slide_container.height(), slides ...

Is it possible to modify the CSS injected by an Angular Directive?

Is there a way to override the CSS generated by an Angular directive? Take, for instance, when we apply the sort directive to the material data table. This can result in issues like altering the layout of the column header. Attempting to override the CSS ...

Guide to sending multiple forms from an HTML page to another PHP page

What is the best way to submit multiple forms on a single HTML page to a PHP page and process all the form elements in PHP upon clicking one button? Any suggestions are appreciated. Thank you in advance. <form id="form1" name="form1"action="abc.php" ...

Unearthing the worth of the closest button that was clicked

I am currently working on a profile management feature where I need to add students to the teacher's database. To achieve this, I am using jQuery and AJAX. However, I am encountering an issue where clicking the "add" button for each student listed on ...

JQuery Ajax: Issue with updating Total Likes for posts when Like button is clicked

There are two JQuery Ajax calls in this scenario. The first call retrieves user messages from MySQL, along with the number of likes for each message and a button for users to like the message. This initial request is functioning correctly. The second Ajax ...

Utilize custom SMTP with JavaScript to dispatch emails

I'm wondering if it is possible to send emails using just JavaScript (I am working on a PhoneGap app). I envision a scenario where I can connect to a specific SMTP server with a login and password, and then send emails using that connection. I have al ...

Inquiries regarding jQuery's functionality and efficiency

I was wondering about the best way to improve performance? Would it be more efficient to have two images written in HTML, one visible and one hidden, and then use jQuery to toggle their display (hiding the visible one and showing the hidden one)? <img ...