angular2 rc5升级,迭代mongo游标失败

时间:2016-09-13 10:48:32

标签: mongodb angular angular-meteor angular2-rc5

我正在写一个meteor / angular2应用程序,我刚刚升级到rc5。当我尝试在组件中显示基于mongo游标的消息列表时出错。我试图提取代码,请告诉我,如果你需要更多的东西。

以下是组件:

import { Component, OnInit } from '@angular/core';
import { Mongo } from 'meteor/mongo';

import template from './messages-list.component.html';

@Component({
  selector: 'messages-list',
  template
})

export class MessagesListComponent implements OnInit {
  messages: Mongo.Cursor<string>;
  collection: Mongo.Collection<string>

  ngOnInit() {
    // just create an empty local collection for brevity, but it also fails with a named client/server collections 
    this.collection = new Mongo.Collection<string>(null);
    this.messages = this.collection.find();    
  }
}

这是html模板:

<ul>
  <li *ngFor="let message of messages">
    {{message}}
  </li>
</ul>

当我尝试查看页面时出现长堆栈跟踪失败,以及几个嵌套错误,但我认为这似乎是它的根源:

Unhandled Promise rejection: (7)
"EXCEPTION: Error in ./MessagesListComponent class MessagesListComponent - inline template:0:10
ORIGINAL EXCEPTION: TypeError: undefined is not an object (evaluating 'async_1.ObservableWrapper.subscribe')

我可以通过更改视图或组件代码再次使代码工作:

查看:

<ul>
  <!-- avoid iterating over messages -->
  <!--li *ngFor="let message of messages">
    {{message}}
  </li-->
</ul>

代码:

// use a normal array instead of a cursor
export class MessagesListComponent implements OnInit {
  messages: string[]

  ngOnInit() {
    this.messages = []
  }
}

以下是我的package.json的依赖项:

"dependencies": {
  "@angular/common": "2.0.0-rc.5",
  "@angular/compiler": "2.0.0-rc.5",
  "@angular/core": "2.0.0-rc.5",
  "@angular/forms": "0.3.0",
  "@angular/platform-browser": "2.0.0-rc.5",
  "@angular/platform-browser-dynamic": "2.0.0-rc.5",
  "@angular/router": "3.0.0-rc.1",
  "angular2-meteor": "0.6.2",
  "angular2-meteor-auto-bootstrap": "0.6.0",
  "angular2-meteor-polyfills": "0.1.1",
  "angular2-meteor-tests-polyfills": "0.0.2",
  "es6-shim": "0.35.1",
  "material-design-lite": "^1.2.0",
  "meteor-node-stubs": "0.2.3",
  "reflect-metadata": "0.1.3",
  "rxjs": "5.0.0-beta.6",
  "zone.js": "0.6.12"
}

有没有人知道如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

我面临着同样的问题,你可以看到here

首先我收到错误消息“NgFor仅支持绑定到Iterables,例如Arrays。”,因为我试图迭代Mongo.Cursor。

我发现的唯一想法是获取光标并使用数组。但阵列中没有包含任何数据,因此我得出结论,客户端可能没有可用的数据,这是一个流星问题。也许我错了,只有另一种方法来转换光标中的数据。

今天我也试图根据this file解决这个问题,但是设置这个项目失败了。

不幸的是,根据Angular 2 RC 5和meteor mongo游标,我没有找到任何有用且有效的例子,但我继续搜索。

相关问题