0

I am very new to mongodb. I am basically trying to retrieve data from a collection and put to screen. Its an angular2-meteor app. I can insert and retrieve data inside the mongo shell. But when I try to loop through the value bookmarks i get the error:

Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

When i console log the returned data from the find() method, it returns the entire mongodb objects collection and the boomarks collection is deeply buried inside the object.

How can I get an array of all the objects inside the returned bookmarks variable returned using the .find() method?

My component is as below:

import { Component } from '@angular/core';
import template from './bookmarks.component.html';
import { Bookmarks } from '../../../../collections/bookmarks';
import { Mongo } from 'meteor/mongo';
@Component({
  selector: 'bookmarks-list',
  template
})
export class bookmarksListComponent {
  bookmarks: Mongo.Cursor<Object>;
  constructor() {
    this.bookmarks=Bookmarks.find();
    console.log(this.bookmarks);
  }
}

here is the html template:

<tbody>
    <tr *ngFor="let bookmark of bookmarks">
        <td>{{bookmark.title}}</td>
        <td>{{bookmark.url}}</td>
        <td>{{bookmark.category}}</td>
        <td><button class="btn btn-danger">Delete</button></td>
    </tr>
</tbody>

Error Snapshot

2 Answers 2

2

Found out the solution using rxjs: Instead of working with meteor/mongo, use rxjs and subscribe to the collections.find() method of mongo. Also while newing up the mongo collection use MongoObservable.collections entire code:

bookmarks.component.ts:

import "reflect-metadata";
import { Component, OnInit, NgZone } from '@angular/core';
import template from './bookmarks.component.html';
import { Bookmarks, bookmark } from '../../../../collections/bookmarks';
import { Observable } from 'rxjs';
@Component({
  selector: 'bookmarks-list',
  template
})
export class bookmarksListComponent implements OnInit{
  private bookmarks: bookmark[];
  constructor(private zone: NgZone) {
  }
  ngOnInit(){
    Bookmarks.find({}).zone().subscribe({
      next: bookmarks => {
        console.log("Got Bookmarks: ", bookmarks);
        this.bookmarks=bookmarks;
      }
    });
  }
}

Bookmarks.ts (collections)

import { MongoObservable } from 'meteor-rxjs';
export interface bookmark {
    title: string;
    url: string;
    category: string;
}
export const Bookmarks= new MongoObservable.Collection<bookmark>('bookmarks');

Angular2-rxjs-meteor: Get help from this repo https://github.com/ijager/meteor-rxjs-angular2.0.0-example.git

Sign up to request clarification or add additional context in comments.

1 Comment

Aint the syntax wrong ? Bookmarks.find({}).zone().subscribe({ why the { ?
0

if you really want to do it using Mongo.Cursor then in your code

constructor() {
this.bookmarks=Bookmarks.find();
console.log(this.bookmarks);
  }

replace this.bookmarks=Bookmarks.find();

with this.bookmarks=Bookmarks.find({}).fetch();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.