Tinie Sluijter
Tinie Sluijter

Reputation: 155

Use id from collection insert within a meteor method

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?

Upvotes: 2

Views: 228

Answers (1)

Amit kumar
Amit kumar

Reputation: 6147

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.

Upvotes: 1

Related Questions