When the user adds a new entry in the client, I need to make a web service call from the server (the client-side code will not have access) and add some additional information to the data stored in MongoDB. Trying to use the standard Meteor.methods/Meteor.call pattern does not seem to work.
Do I need to listen for the click event on the "Add" button on both the server and the client? Should I raise a custom event on the client that the server reacts to? Is there a proper way to make a direct call to a server-side method? Most importantly, how do I keep TypeScript happy in all of this?
I am new to the TypeScript layer on top of Meteor and it is throwing me for loop. I have been generally following the Angular-Meteor tutorial for 2.0 but this sort of thing is not covered yet.
Using angular2, Meteor and Typescript, what works is to chain the Meteor.methods.
First on the client, in response to a button click ...
Meteor.call('importCsv',id,function(error,result) { ...
In collections/methods folder or similar, I define the method as follows:
});
In server/ folder, a file includes the method as follows
....
In server/main.ts I import the collections/methods/filename. In client/app.ts I import the same thing. The client Meteor.call successfully calls the first method which then calls the second one in the server/ folder.
My goal is to have a bunch of processing on the server initiated by the client. When I had the function calls in the method defined in collections/methods imported into both the client and server, it resulted in compiler errors.
Angular2-Meteor issue 74
Yes, you can call directly from the server to the web service in order to receive data. I am not so sure how you did for the
Meteor.methods/Meteor.call
and say it did not work. But basically, the idea is client will click the button and then button will trigger a method on the server. The server method then will call the web service and return the data.Some example code could be:
The tricky part for new comer when calling the rest is you need to use
aysnc
calling in order to return the data to client. We normally make helper function for thatAnd call the helper like this
This way, the
res
in the client code above will get the result.I actually dropped the Angular 2 for the lack of documentation, but stay with Typescript for my system because I can wrap all the meteor call inside the Typescript class, as you can see in my example,
serverMethod
is in the typescript function format, not in meteor way likeMeteor.methods({....})
, which is really good for nowTypically, this is a class in my server folder
I have a class to map the typescript service to the
meteor
method