无法使用带角度2流星的ngFor将Mongodb集合数据显示为angular2

时间:2016-09-25 17:03:00

标签: mongodb angular angular2-meteor

我对mongodb很新。我基本上试图从集合中检索数据并将其放到屏幕上。它是一个angular2-meteor应用程序。我可以在mongo shell中插入和检索数据。但是当我尝试遍历值书签时,我得到了错误:

  

找不到不同的支持对象' [object Object]'类型'对象'。 NgFor仅支持绑定到Iterables,例如Arrays。

当我在控制台中记录从find()方法返回的数据时,它返回整个mongodb对象集合,并且boomarks集合深埋在对象中。

如何使用.find()方法获取返回的书签变量中的所有对象数组?

我的组件如下:

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);
  }
}

这是html模板:

<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 个答案:

答案 0 :(得分:2)

使用rxjs找到解决方案: 而不是使用meteor / mongo,使用rxjs并订阅mongo的collections.find()方法。此外,在新建mongo集合时,使用MongoObservable.collections整个代码:

<强> 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(馆藏)

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

Angular2-rxjs-流星: 从此repo https://github.com/ijager/meteor-rxjs-angular2.0.0-example.git

获取帮助

答案 1 :(得分:0)

如果你真的想使用Mongo.Cursor然后在你的代码中

foreach($array as $key => $person)
{
  $name = $person['name'];
  echo $name;
}

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

this.bookmarks=Bookmarks.find();