Meteor:使用不同的参数多次调用外部API并发布数据

时间:2022-09-29 19:42:14

I have a requirement where i need to call the same external API multiple times but with different parameter every time, every call result should be shown in tabular form in the UI and all those results should be inserted into db.

我有一个要求,我需要多次调用相同的外部API但每次都有不同的参数,每个调用结果应该在UI中以表格形式显示,所有这些结果应该插入到db中。

What is the best way to do so that there is no delay in the UI ?

在UI中没有延迟的最佳方法是什么?

This is what i am trying as of now

这就是我现在所想的

Server Code :

服务器代码:

Meteor.startup(function () {
    Meteor.methods({
      getLocation: function(){
          this.unblock();
          var userAddress = [1,2,3,4,5,6,7];
          for(var i=0;i<userAddress;i++){
            var response = HTTP.call("GET","URL"+userMacAddress[i]+"/track");
            if(response){
              result = response.data;
              var dateTime = new Date();
              DeviceView.insert({
                  'dateTime' : dateTime,
                  'aps' : result.aps|| '',
                  'Mac' : result.Mac
              });
              //keep pushing the repsone to array
            }
          }
        }
        //return the array
})
});

1 个解决方案

#1


0  

First of all, no need to put the Meteor.methods() block inside Meteor.startup().

首先,不需要将Meteor.methods()块放在Meteor.startup()中。

I'm not exactly sure what type of "delay" you're trying to prevent. Given that you're using the synchronous version of HTTP.call I'm going to assume this is a server-side method.

我不确定你试图阻止什么类型的“延迟”。鉴于您正在使用HTTP.call的同步版本,我将假设这是一个服务器端方法。

If the results are going to be displayed on the page, you can simply use an {{#each}}{{/each}} block in your template and the results will automatically populate on the page as soon as they are inserted and you don't neccessarily need to return the array from the method for each document to appear in the UI.

如果结果将显示在页面上,您只需在模板中使用{{#each}} {{/ each}}块,一旦插入结果,结果就会自动填充到页面上不一定需要从方法中返回数组,以便在UI中显示每个文档。

Say for instance that you want to display the results in an unordered list, in your template file you do something like this:

比如说你想在无序列表中显示结果,在模板文件中你可以这样做:

<template name="myTemplate">
    ...
    <ul>
    {{#each devices}}
    {{> device}}
    {{/each}}
    </ul>
    ...
    </template>

This will pass the context of each item to the child template "device", so you can make a template named "device":

这会将每个项目的上下文传递给子模板“device”,因此您可以创建名为“device”的模板:

<template name="device">
<li> The Mac Address is: {{Mac}} {{#if aps}} The APS is: {{aps}} Recorded at: {{dateTime}}</li>
</template>

In your JS you will need

在你的JS中你需要

Template.myTemplate.helpers({
    devices: function() {
       return DeviceView.find({});
    }
});

As soon as each insert is fired, the <li> element for that record will be inserted into the DOM automatically.

一旦触发每个插入,该记录的

  • 元素将自动插入到DOM中。

  • Keep in mind that the results can only get returned as quickly as each HTTP.get request returns. Any delay in any one of the calls will cause a delay before that result (and the next) will appear.

    请记住,结果只能在每个HTTP.get请求返回时尽快返回。任何一个呼叫中的任何延迟都会导致在该结果(和下一个)出现之前出现延迟。

    If you don't mind using a "Loading Results" sort of thing with a preloader you could use css display:none or opacity:0 on the container holding the results and then use a function in the method callback to hide the preload message and show the results once the result returns.

    如果您不介意使用预加载器的“加载结果”类型的东西,您可以在容纳结果的容器上使用css display:none或opacity:0,然后在方法回调中使用函数来隐藏预加载消息和结果返回后显示结果。

    #1


    0  

    First of all, no need to put the Meteor.methods() block inside Meteor.startup().

    首先,不需要将Meteor.methods()块放在Meteor.startup()中。

    I'm not exactly sure what type of "delay" you're trying to prevent. Given that you're using the synchronous version of HTTP.call I'm going to assume this is a server-side method.

    我不确定你试图阻止什么类型的“延迟”。鉴于您正在使用HTTP.call的同步版本,我将假设这是一个服务器端方法。

    If the results are going to be displayed on the page, you can simply use an {{#each}}{{/each}} block in your template and the results will automatically populate on the page as soon as they are inserted and you don't neccessarily need to return the array from the method for each document to appear in the UI.

    如果结果将显示在页面上,您只需在模板中使用{{#each}} {{/ each}}块,一旦插入结果,结果就会自动填充到页面上不一定需要从方法中返回数组,以便在UI中显示每个文档。

    Say for instance that you want to display the results in an unordered list, in your template file you do something like this:

    比如说你想在无序列表中显示结果,在模板文件中你可以这样做:

    <template name="myTemplate">
        ...
        <ul>
        {{#each devices}}
        {{> device}}
        {{/each}}
        </ul>
        ...
        </template>
    

    This will pass the context of each item to the child template "device", so you can make a template named "device":

    这会将每个项目的上下文传递给子模板“device”,因此您可以创建名为“device”的模板:

    <template name="device">
    <li> The Mac Address is: {{Mac}} {{#if aps}} The APS is: {{aps}} Recorded at: {{dateTime}}</li>
    </template>
    

    In your JS you will need

    在你的JS中你需要

    Template.myTemplate.helpers({
        devices: function() {
           return DeviceView.find({});
        }
    });
    

    As soon as each insert is fired, the <li> element for that record will be inserted into the DOM automatically.

    一旦触发每个插入,该记录的

  • 元素将自动插入到DOM中。

  • Keep in mind that the results can only get returned as quickly as each HTTP.get request returns. Any delay in any one of the calls will cause a delay before that result (and the next) will appear.

    请记住,结果只能在每个HTTP.get请求返回时尽快返回。任何一个呼叫中的任何延迟都会导致在该结果(和下一个)出现之前出现延迟。

    If you don't mind using a "Loading Results" sort of thing with a preloader you could use css display:none or opacity:0 on the container holding the results and then use a function in the method callback to hide the preload message and show the results once the result returns.

    如果您不介意使用预加载器的“加载结果”类型的东西,您可以在容纳结果的容器上使用css display:none或opacity:0,然后在方法回调中使用函数来隐藏预加载消息和结果返回后显示结果。