Search code examples
meteortypescriptangularangular2-meteor

Using Meteor with Angular2/Typescript, how do I call an external web service from server-side code based on a client-side event?


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.


Solution

  • 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:

    Meteor.methods({
     'importCsv': function(id) {
        console.log('importCsv method on client');
        Meteor.call('importCsvServer',id);
    
     }
    

    });

    In server/ folder, a file includes the method as follows

    Meteor.methods({
    'importCsvServer': function(id) {
    

    ....

    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