Unraveling the Mystery of Reading AnonymousSubject in Angular 2

Currently, I am debugging my ng2 application using console logs. When logging an array, it shows an AnonymousSubject with the following attributes:

AnonymousSubject
_isScalar:false
closed:false
destination:AnonymousSubject
hasError:false
isStopped:false
observers:Array[0]
operator:ToArrayOperator
source:AnonymousSubject
thrownError:null
__proto__:Subject

Is it possible to view the content of the array? Why does clicking 'destination' lead to recursive looping of its content?

Code:

GetFeed(id: UUID): Feed[] {    
    var toReturnFeed: any[];

    this.table.find({id: id}).fetch().subscribe(result => toReturnFeed);
    console.log(toReturnFeed);

    return toReturnFeed;
}

I have more questions, but I'll save them for later. Thank you in advance.

Answer №1

The data being logged is not an actual array, but an Observable that is expected to return an array.

Due to the way you set up this Observable, it creates a cyclic reference as the destination is the same as the origin.

To log the array:

this.someObservable.subscribe((someArray: any[]) => {
    console.log(someArray);
});

From the code you provided, it seems you may be overlooking a crucial aspect of asynchronous methods. You cannot return toReturnFeed directly as it will always be undefined. Instead, you should return the Observable<Feed[]>

GetFeed(id: UUID): Observable<Feed[]> { 
    return this.table.find({id: id}).fetch()
}

You can then invoke this method from another part of your code and retrieve the results like this:

serviceInstance.GetFeed(1).subscribe((returnFeed: Feed[]) => {
    console.log(returnFeed);
});

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

Can you guide me on how to generate a personalized stamp within the Angular component using Grapecity GcPdfViewer?

When a Pdf is opened in the GcPdfViewer by the User, they are required to stamp the Pdf with an image located in the src/assets folder. However, the example provided on this webpage is not functioning correctly. Can anyone offer me a functioning example? ...

A generic function in Typescript that can accept an object with the first argument as a specified key

I am looking to create a function that will take a string as its first argument and will only accept a second argument of type object if it contains the first argument as a key with a boolean value: const checkFlag = (str:string, obj) => obj[str] Alth ...

Ways to connect a JSON object with an Angular material form

Currently, I am working on a basic form using the Angular framework with a provided JSON file. The JSON file contains arrays and objects, and I have successfully bound the arrays to my form. However, I am facing an issue with binding the JSON object. The c ...

Authenticating passports without the need for a templating engine

Seeking assistance with integrating Passport authentication using the Google strategy. Authentication with Google works, but upon returning to the express server, I aim to redirect to a client-side page along with profile data without utilizing a templatin ...

Arrange the columns in Angular Material Table in various directions

Is there a way to sort all columns in an Angular material table by descending order, while keeping the active column sorted in ascending order? I have been trying to achieve this using the code below: @ViewChild(MatSort) sort: MatSort; <table matSort ...

Tips for updating state in React TypeScript 2.0?

Working with a small component built using React and TypeScript has presented a unique challenge. interface Props { } interface State { isOpen: boolean; } class App extends React.Component<Props, State> { constructor(props: Props) { super ...

What is the process for loading a feature module (lazyloaded module) in Angular without server-side rendering (SSR

Calling all Angular developers! I have a project where I'm using Angular Universal (SSR) to render a lot of routes on the server-side. However, I have some private routes and feature modules that are better off being rendered on the client-side. Speci ...

Ensuring the accuracy of forms using third-party verification services

While working on an Angular form validation using an external service, I encountered a cannot read property of undefined error. The component contains a simple form setup: this.myForm = this.fb.group({ username: ['', [this.validator.username] ...

Utilizing index signatures in TypeScript mapped types

Is there a way to abstract over the type { 'k': number, [s: string]: any } by creating a type alias T such that T<'k', number> results in the desired type? Take a look at the following example: function f(x: { 'k': numb ...

What is the recommended module to choose when importing MAT_DIALOG_DATA?

When working on my Angular project, I noticed that there are multiple options available for importing MAT_DIALOG_DATA. I have tried importing both @angular/material and @angular/material/dialog, and found that both options work successfully. However, I a ...

What is the proper way to implement Sequelize JOINs while using TypeScript?

Seeking guidance on how to properly JOIN two tables in Sequelize: const results = await MainModel.findAll({ include: [ JoinedModel ] }); Struggling with TypeScript's complaints when trying to access data from the joined table: const joinedField = r ...

Angular: it is impossible to access property 'x' as it is undefined

I am facing an issue where I keep getting the error message Cannot read property 'id' of undefined in my HTML component. <button (click)="getRecipeDetails()">Show</button> <div> <div [innerHTML]="recipeIn ...

Create your own Angular control - rate stars - with dynamic input values

<div class="rating"> <div style="display: inline-block" *ngFor="let starred of stars; let i = index" (click)="rate(i + (starred ? (value > i + 1 ? 1 : 0) : 1))"> <ng-container *ngIf="starred; else noStar"><mat-icon class=" ...

Challenges with displaying css/bootstrap classes within angular2

Experiencing difficulties with CSS/Bootstrap integration when displayed in an angular2 component. When attempting to display the CSS and HTML content in the Index.html file (with proper CSS and JS references), everything functions correctly. However, once ...

Can you explain the significance of <any> when used before a Typescript class constructor parameter?

Currently, I am immersing myself in the Angular - Testing documentation. While going through the section on testing asynchronous services (specifically HTTP services), I came across a class constructor containing an <any> right before the passed argu ...

What is the best way to loop through an array that contains a custom data type

When I declared the type: export interface Type{ id: number; name: string; } I attempted to iterate over an array of this type: for(var t of types) // types = Type[] { console.log(t.id); } However, I encountered the following error message: ...

Unexpected behavior: ng2-dragula modelDrop event leading to undefined array

Struggling to figure out the issue with this code. As shown in this GIF, when dragging a div from one container to another, the object disappears and the array becomes undefined. https://i.stack.imgur.com/TELyc.gif Here is the code snippet: Main View.ht ...

Combining 2 dates into a single cell format

I'm struggling to change the date format of two dates displayed in one cell using ag-grid. I have tried creating a new function called dateFormatterr with two parameters, but it doesn't seem to work. Below is a snippet of my code and a screenshot ...

Contrasting router and router-deprecated in angular2

After upgrading from version "beta.17" to "2.0.0-rc.1", I am a bit confused about when to use router and when to use router-deprecated. Can someone help clarify this for me? ...

Encountering "Module ts-jest not found in the transform option" Error During Bazel Testing

In my working Bazel BUILD file, I have set up the following configurations: package(default_visibility = ["//visibility:public"]) load("@io_bazel_rules_docker//nodejs:image.bzl", "nodejs_image") load("@npm_bazel_typescript//:index.bzl", "ts_library") # ...