Use id from collection insert within a meteor meth

2019-02-28 20:55发布

问题:

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.