Use id from collection insert within a meteor method

ぃ、小莉子 提交于 2019-12-02 06:45:46

问题


Within my Angular / Meteor app I want to use the created id from a collection insert in a meteor method in the client.

Within a client Angular component the following method exists.

onSubmit(): void {
  Meteor.call('insertItem', this.item, (error, response) => {
    if (error) {
      ...
    } else {
      this.ngZone.run(() => {
        this.router.navigate(['/item/manage', response]);
      });
    }
  });
} 

The meteor method that's called by this method looks like following.

insertItem(newItem: Item): Observable<string> {
    return Items.insert(newItem);
}

I want to use the id that's returned after the insert. This id is an Observable, but the response in the meteor method call doesn't recognize it as an Observable.

In what way can I return the Id to the client and use it within a router navigate?


回答1:


I think the error is on the server-side meteor method:

Items.collection.insert(newItem)

With the added .collection it will return the new _id-string.



来源:https://stackoverflow.com/questions/41706273/use-id-from-collection-insert-within-a-meteor-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!