结合使用Meteor和Angular2/Typescript,如何基于客户端事件从服务器端代码调用外部Web服务? [英] Using Meteor with Angular2/Typescript, how do I call an external web service from server-side code based on a client-side event?

查看:120
本文介绍了结合使用Meteor和Angular2/Typescript,如何基于客户端事件从服务器端代码调用外部Web服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户在客户端中添加新条目时,我需要从服务器进行Web服务调用(客户端代码将无权访问),并将一些其他信息添加到MongoDB中存储的数据中.尝试使用标准的Meteor.methods/Meteor.call模式似乎无效.

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.

我是否需要监听服务器和客户端上添加"按钮上的click事件?我应该在服务器响应的客户端上引发自定义事件吗?是否有适当的方法可以直接调用服务器端方法?最重要的是,如何使TypeScript在所有这些方面感到高兴?

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?

我是Meteor上TypeScript层的新手,它使我陷入循环.我通常遵循2.0的Angular-Meteor教程,但是这种情况尚未涵盖.

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.

推荐答案

使用angular2,Meteor和Typescript,有效的方法是链接Meteor.methods.

Using angular2, Meteor and Typescript, what works is to chain the Meteor.methods.

首先在客户端上,响应按钮单击...
Meteor.call('importCsv',id,function(error,result){...

First on the client, in response to a button click ...
Meteor.call('importCsv',id,function(error,result) { ...

在collections/methods文件夹或类似文件夹中,我将方法定义如下:

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) {

....

在server/main.ts中,我导入集合/方法/文件名.在client/app.ts中,我导入相同的内容.客户端Meteor.call成功调用第一个方法,然后在服务器/文件夹中调用第二个方法.

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问题74

这篇关于结合使用Meteor和Angular2/Typescript,如何基于客户端事件从服务器端代码调用外部Web服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆