首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >返回Meteor.http结果的方法

返回Meteor.http结果的方法
EN

Stack Overflow用户
提问于 2014-09-04 20:48:17
回答 3查看 4.3K关注 0票数 5

我有一个围绕http.get的Meteor方法。我试图将该http.get的结果返回到该方法的返回中,以便在调用该方法时可以使用该结果。

但我不能让它起作用。

这是我的密码:

(在共享文件夹中)

代码语言:javascript
运行
复制
Meteor.methods({
    getWeather: function(zip) {
        console.log('getting weather');
        var credentials = {
            client_id: "string",
            client_secret: "otherstring"
        }

        var zipcode = zip;

        var weatherUrl = "http://api.aerisapi.com/places/postalcodes/" + zipcode + "?client_id=" + credentials.client_id + "&client_secret=" + credentials.client_secret;

        weather = Meteor.http.get(weatherUrl, function (error, result) {
            if(error) {
                console.log('http get FAILED!');
            }
            else {
                console.log('http get SUCCES');
                if (result.statusCode === 200) {
                    console.log('Status code = 200!');
                    console.log(result.content);

                    return result.content;
                }
            }
        });
        return weather;
    }
});

由于某些原因,即使存在结果,也不会返回结果,并且http工作:console.log(result.content);确实记录结果。

(客户端文件夹)

代码语言:javascript
运行
复制
  Meteor.call('getWeather', somezipcode, function(error, results) {
     if (error)
        return alert(error.reason);

     Session.set('weatherResults', results);
  });

当然,在这里,会话变量最终是空的。

(请注意,如果我用方法中的某个虚拟字符串硬编码返回,代码的这一部分似乎很好,因为它适当地返回了。)

帮助?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-09-04 20:54:13

在您的示例中,Meteor.http.get是异步执行的。

见文档

HTTP.call(方法、url、选项)

在服务器上,可以同步或异步运行此函数。如果省略回调,则该回调将同步运行,并在请求成功完成后返回结果。如果请求未成功,则引发错误。

通过删除asyncCallback切换到同步模式:

代码语言:javascript
运行
复制
try {
  var result = HTTP.get( weatherUrl );
  var weather = result.content;
} catch(e) {
  console.log( "Cannot get weather data...", e );
}
票数 14
EN

Stack Overflow用户

发布于 2014-09-04 21:57:41

Kuba是正确的,但您仍然可以异步调用HTTP.get,并使用将来停止返回方法,直到get响应为止:

代码语言:javascript
运行
复制
var Future = Npm.require('fibers/future');

Meteor.methods({
    getWeather: function(zip) {
        console.log('getting weather');
        var weather = new Future();
        var credentials = {
            client_id: "string",
            client_secret: "otherstring"
        }

        var zipcode = zip;

        var weatherUrl = "http://api.aerisapi.com/places/postalcodes/" + zipcode + "?client_id=" + credentials.client_id + "&client_secret=" + credentials.client_secret;

        HTTP.get(weatherUrl, function (error, result) {
            if(error) {
                console.log('http get FAILED!');
                weather.throw(error);
            }
            else {
                console.log('http get SUCCES');
                if (result.statusCode === 200) {
                    console.log('Status code = 200!');
                    console.log(result.content);

                    weather.return(result);
                }
            }
        });
        weather.wait();
    }
});

在这种情况下,这种方法与同步get相比并没有太大的优势,但是如果您在服务器上做了一些可以从异步运行的HTTP调用中受益的事情(因此不会阻塞方法中的其余代码),但是您仍然需要等待该调用在方法能够返回之前返回,那么这是正确的解决方案。例如,您需要执行多个非特定的get,如果同步执行,则所有这些都必须等待对方一个接一个地返回。

更多的这里

票数 2
EN

Stack Overflow用户

发布于 2017-05-02 19:15:47

有时异步调用更好。为此,您可以使用异步/等待语法,并且需要对HTTP.get进行编程。

代码语言:javascript
运行
复制
import { Meteor } from 'meteor/meteor';
import { HTTP } from 'meteor/http';

const httpGetAsync = (url, options) =>
    new Promise((resolve, reject) => {
        HTTP.get(url, options, (err, result) => {
            if (err) {
                reject(err);
            } else {
                resolve(result);
            }
        });
    });

Meteor.methods({
    async 'test'({ url, options }) {
        try {
            const response = await httpGetAsync(url, options);
            return response;
        } catch (ex) {
            throw new Meteor.Error('some-error', 'An error has happened');
        }
    },
});

注意,流星test方法被标记为async。这允许在其中使用await操作符来调用返回Promise的方法。await操作符后面的代码行在返回的承诺被解决之前不会被执行。如果承诺被拒绝,catch块将被执行。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25674736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档