Issue with object property not being recognized in Vue script, despite working fine in template

Currently, I am engaged in an exciting project using Vue. Within my Firestore database, I have a document named "default" within the collection "impostazioni" structured like this:

impostazioni/default = { 
  iniziata: false,
  finita: false,
  password: "abcd"
}

My goal is to retrieve each individual property and store them within my component. Here's how I've attempted to achieve this:

<template>
{{impostazioni}}
</template>

<script>

export default {
    name: "login",
    data(){
            impostazioni: null
        }
    },
    firestore: {
        impostazioni: db.collection('impostazioni').doc('default')
    }
 }
    </script>

When I follow this approach, the template correctly displays the expected output:

{ "finita": false, "iniziata": false, "password": "abcd" }

However, when initializing the data differently:

data(){
  return {
    iniziata: null,
    finita: null,
    password: null,
  }
},
firestore : {
  iniziata: db.collection('impostazioni').doc('default')['iniziata'],
  finita: db.collection('impostazioni').doc('default')['finita'],
  password: db.collection('impostazioni').doc('default')['password']
}
}

and then attempting to display them in the template:

{{finita}} - {{iniziata}} - {{password}}

This method does not work as intended. It throws an error suggesting that

db.collection('impostazioni').doc('default')
is undefined.

On the other hand, if I store the entire document in a single variable "impostazioni" and access its properties like {{impostazioni.password}}, I do obtain the correct value.

Is there a way for me to extract and store the different elements of the document into separate variables?

Answer №1

When working with Firestore, it is important to remember that the data retrieval may not be immediate and you may need to handle promises properly.

To ensure that you access the fields only after the promise has resolved, you can either explicitly wait for it or utilize Vue's computed properties to dynamically update separate variables:

computed: {
  iniziata() {
    if(this.impostazioni)
      return this.impostazioni
    else
      return '' // set a default value here
  }
  ...
}

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

Fundamental Guidelines for Firebase

I've recently started working with Firebase and I'm encountering some issues with the security rules. My project is a blog website where visitors can read posts, users, and comments without being logged in. However, logged-in and verified users ...

Assigning a modal value to an HTML field

I am in need of a solution for multiple HTML fields with a common modal attached to each one. The code I have currently looks like this: <!-- First Element --> <div class="box box-element ui-draggable"> <span class="config ...

Iterating through a JSON object to verify the presence of a specific value

I have a JSON Object and I am looking for a way in Angular 6 to search for the value "Tennis" in the key "name". Can you provide guidance on how to achieve this? { "id":2, "name":"Sports", "url":"/sports" "children":[ { "id":1, ...

What is the best way to divide this string with jQuery?

Is there a way to use jQuery to split this specific string? "[10.072721346470422,76.32974624633789][[10.075854059674523,76.32043361663818],[10.073650930297095,76.32888793945312],[10.074918540288232,76.33090496063231],[10.073862198974942,76.33137702941895] ...

How can I manipulate image resolution and export images of varying sizes using Javascript?

I am new to using three js. I have successfully exported an image in png format using the WebGL renderer. However, when attempting to export the image in a different custom size resolution, the resolution does not change. How can I set the image resoluti ...

Is there any specific reason not to use a short HTML/CSS syntax like <div a b c></div>?

When it comes to writing HTML in less standard environments, such as a phonegap app where strict syntax and semantics are not crucial, I have developed a unique approach that works for me. I aim to keep my HTML code short and concise by using the followin ...

Using VueJS to apply filters to an object causes a decrease in my application's performance

Recently, I've been working on implementing a filter for a search bar. However, I've noticed that whenever I input something into the search bar, there is a noticeable delay as it loads the entries. I'm not sure if this issue is related to ...

Vue paired with Rainyday.js

I attempted to incorporate Vue with rainyday.js by following different resources, but unfortunately could not find any relevant information. Can anyone provide guidance on how to successfully implement rainyday.js with Vue? <body onload="run();"> ...

I encountered an issue with generating the render function when utilizing the tree component in ElementUI

I am encountering issues while trying to use Element UI in IE 11, specifically with the tree component. The following error message is being displayed: Failed to generate render function: SyntaxError 'missing in' In addition, I would like to d ...

How come this variable isn't recognized as 0 even though the debugger is indicating otherwise?

I am currently working on a React component that receives the total number of reviews as a prop. In cases where the number of reviews is 0, I intend to display an element indicating <div>No reviews yet.</div> If there are reviews available, I ...

Unable to retrieve real-time data from Firestore using getStaticPaths in Next.js

While fetching data from Firebase Firestore using getStaticProps is successful, I encounter a 404 page when attempting to implement the logic for retrieving details of individual items with getStaticPaths. The current state of my [id].js code appears as fo ...

Having trouble with displaying the modal dialog in Twitter Bootstrap 3?

There seems to be an issue with my Twitter Bootstrap modal as it is not rendering the dialog box correctly, and I'm puzzled about the reason behind this. HTML <p class="text-center btn-p"><button class="btn btn-warning" data-toggle="modal" ...

What are the necessary headers that must accompany a post request?

While testing the server with Postman, everything seems to be working fine as I receive a response: https://i.stack.imgur.com/yMRfj.png However, when attempting to make a POST request from the browser using the same address, it results in an error and th ...

Material-UI React is unable to identify the `underlineStyle` property on a specific HTML element

I tried implementing a custom style to change the underline color of a material-UI TextField element, following an example I found. http://www.material-ui.com/#/components/text-field Unfortunately, when I attempt to add my own styling, React does not rec ...

accessing information from webpage using hyperlink reference

This seems to be a rather straightforward issue, but unfortunately, I lack the necessary expertise to address it. Despite conducting thorough research, I have been unable to find a solution - mainly because I am uncertain about what specific terms or techn ...

`Angular2 - exploring the complexities of function scope`

I'm facing a challenge while working on my Angular2 Sample with the http module. Here is a snippet from my component: app.loginComponent = ng.core.Component({ selector: 'login', templateUrl: 'app/login/login.html&ap ...

What is the best way to retrieve information from a different table in order to establish a condition and form a relationship in Laravel and Vue?

Seeking assistance with website development. How can I retrieve data from the cycles table to establish a conditional relationship with the mortalities table? The information in the cycles table includes: $table->increments('id'); $table ...

Unable to render Blender model in threeJS scene due to undefined condition

I've tried following solutions from Stack Overflow to resolve this issue, but I'm still unable to load external objects (Blender). Essentially, I exported it as a ThreeJS JSON file, here is the content of my JSON file: { "textures":[], ...

Having Trouble Sending Text to InputBox Using Selenium WebDriver

Greetings everyone Can someone guide me on how to use Selenium to input a Login and Password in an Alert Dialog Box? Upon loading the webpage, the alert is already displayed: https://i.stack.imgur.com/F1O5S.png I have attempted the following code: Str ...

Submit data from one form to another form located within an iframe

I am currently using a JX Browser that allows content to be displayed in an iframe. My goal is to automatically transfer the username and password of a user logging into my ticketing software to another form within an iframe. The page within the iframe is ...