Tips for properly waiting for an AngularFire2 subscription to complete before executing the subsequent lines of code within Angular2

Having an issue with my Angular2 Type Script code. The goal is to access Questions from a Firebase Database by subscribing to a FirebaseListObserver:

this.af.list('/questions').subscribe( val => {
     this.questions = val
     console.log(this.questions) // working fine
})
console.log(this.questions) // showing as undefined

Struggling to figure out how to wait for the subscription to receive a value before executing a function that relies on this.questions.

Experimented with async/await, but it didn't pause for the subscription to return a value.

Tried subscribing inside a promise as well, still ending up with an empty array.

Answer №1

To incorporate the function, you have two options: execute it within the subscribe callback or convert questions into a setter/getter:

private _questions = [];

set questions(questions) {
  this._questions = questions;

  // Respond to changes in questions here
}

get questions() {
 return this._questions;
}

This setup functions similarly to a watcher in older versions of angular. Within the set function, examine if questions contains a value and respond accordingly by invoking the necessary function.

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

Validate if the translation file exists in ngx-translate

Is there a way to determine if a translation file exists for the language obtained from navigator.language using ngx-translate? I am looking to implement something similar to: if( isLanguageAvailable(navigator.language)) { this.translate.use(navigator.l ...

ngx-capture : Issue with capturing the entire page

Hey there! I'm currently using the ngx-capture package for capturing images, but I've encountered a problem. It seems to only capture the area that is visible in the browser. Is there a way to capture the whole page or an entire div? I came acr ...

Sharing variables between parent and child components in Angular 2 Dart using router

How can I effectively share variables between a parent component with a router and a child component while keeping them updated through bindings? Here is a snippet of the code: app_component.dart: import 'package:angular2/core.dart'; ...

Using Typescript: Defining a property's type based on the value of another property in the same object

I'm dealing with a TypeScript interface that consists of two properties (type:string and args:object). The contents of the args property may vary based on the value of the type. I'm looking for the correct type definition to specify for the args ...

Updating the node startup file with Visual Studio 2015 using NodeJS/Typescript

Encountering a persistent error: Error Code: TS5055 Cannot write file C:/project/dir/server.js' because it would overwrite the input file. Project: TypeScript/JavaScript Virtual Projects Even after renaming my entry filename to nodeserver.js, the ...

Using React, PIXI, and Zustand can sometimes lead to stale state issues when handling mouse events

I currently have a Pixi canvas that utilizes pointer handlers. Users are able to click on a point within a 'sequence' and move it. Recently, I came across an issue with the mouse handlers having stale state. To resolve this, I began recreating t ...

Leverage the power of Bootstrap 4 without incorporating its pre-built components

Is it possible to utilize Bootstrap 4 without its components? The diagram below seems to suggest otherwise, correct? My Scenarios: I need to implement Angular Material components instead of Bootstrap components. It's acceptable to use Bootstrap 4 ...

What is the best way to send parameters to an async validator when working with reactive controls in Angular

Issue with Validation I am facing a challenge while using the same component for both read and edit routines. The async-validator functions perfectly when dealing with new entries. However, a problem arises if the user mistakenly changes a value and then ...

Different ways to contrast rows within ag-grid

I am in the process of comparing two identical rows within my ag-grid and emphasizing any variances between them. While most column values are matching, I wish to highlight any cell that differs from the previous row. Is there a means to achieve this in ...

Is there a way to asynchronously update state in a component without directly triggering an action?

As someone new to React, I'm facing a challenge in updating my Component State after making an asynchronous function call. I have three distinct files: Dashboard.js and GetInfos. The purpose of GetInfos is to handle fetching data, while Dashboard pro ...

What is the reason behind CORS errors being triggered by the inclusion of an HTTP Interceptor?

I am currently building an Angular 6 application that communicates with a PHP REST Api. I am running my development environment on localhost, and in order to avoid CORS errors, I had to enable the Access-Control-Allow-Origin: * setting. Lately, I have bee ...

Changing the default route in Angular 2 based on conditions

I'm currently developing an application where, upon entering the page, the default route for the user is the "Login" page. However, I want to implement a condition based on whether the user has a local storage variable (id) set. If this variable exist ...

Using href with IconButtonProps is not supported

I'm facing a challenge in creating a wrapper for the IconButton. I aim to pass components or href props, but unfortunately, I am unable to achieve this by passing the IconButtonProps. Is there a way to accomplish this? function CustomIconButton(props ...

What is the step-by-step guide for implementing an access limiter on an interface

When attempting to modify the access level on an interface in Typescript, I encountered an error stating 'cannot appear in type member.' After removing the access limiter on the interface and implementing it, I tried changing the access limiter o ...

Tips for adding a border to a table cell without disrupting the overall structure of the table

Looking for a way to dynamically apply borders to cells in my ng table without messing up the alignment. I've tried adjusting cell width, but what I really want is to keep the cells' sizes intact while adding an inner border. Here's the sim ...

Error message: Unable to modify headers after they have already been sent-NodeJS, MongoDB, and Mongoose

While working on a basic todo MEAN application with a remote database, I've encountered a strange issue. Whenever I try to update or delete a record (not always, but mostly), an error is thrown: events.js:160 throw er; // Unhandled 'error& ...

When using *ngFor to loop through components, the ng2-file-upload library's new FileUpload() function may not always create a new instance for each component

I am facing an issue where I have multiple formats that I loop over (using ngFor) to create new components. Each component has the ng2-file-upload implemented through the constructor (new FileUploader()). The problem is, when I add a new file and display ...

Angular material chips showcase a row of five chips

I am working with the basic mat-chips from angular material and I want to arrange them in rows of five without increasing their size: Chip1 Chip2 Chip3 Chip4 Chip5 ..................(space left) Chip6 Chip7 Chip8 Chip9 Chip10 ...............(space le ...

Welcome to Ng-Zorro, your destination for all things innovative and cutting

After including the NG-Zorro library in my project, every page I navigate to within my App displays only the NG-Zorro logo and the message "You have Arrived". I'm curious about removing this logo/text so that I can properly view my pages. I referred ...

Apologies, but it seems there was an issue with the installation of the "@angular/compiler-cli" package

Despite thoroughly searching through various threads, I am still unable to find a solution to my problem. I have cloned the angular2 quickstart project and ensured that all module versions are up to date. For reference, here is the link to the repository ...