【问题标题】:Unable to use npm request package to meteor app无法使用 npm 请求包到流星应用程序
【发布时间】:2016-08-24 01:55:46
【问题描述】:

我在命令行中执行了meteor npm install --save request

我在代码import {request} from 'request'中导入了请求库

并尝试将其与

一起使用
request('http://www.google.com', function (error, response, body) {
    if (!error && response.statusCode == 200) {
      console.log(body) // Show the HTML for the Google homepage.
    }
})

但是我不断收到以下错误:

undefined is not a function

如何在我的流星应用中使用 npm request 包?

【问题讨论】:

    标签: meteor npm


    【解决方案1】:

    请求包的默认导出是您要查找的对象。将您的导入语句更改为以下内容:

    import request from 'request';
    

    您可能需要来自request 的低级功能,但是您的示例也可以使用meteor 的HTTP pacakge(它本身是request 的包装器)来完成。

    这是一个例子:

    import { Meteor } from 'meteor/meteor';
    import { HTTP } from 'meteor/http';
    
    Meteor.startup(() => {
      const resp = HTTP.get('http://www.google.com');
      console.log(resp.content);
    });
    

    请注意,您需要运行 meteor add http 才能使其工作。

    【讨论】:

    • 感谢您的回复。显然我在导入时误解了 {} 的使用。关于 Meteor 的 http 包,我发现它有点难以使用,尤其是当我的 url 指向图像时。 resp.content 似乎是一个字符串。
    猜你喜欢
    • 2014-09-27
    • 1970-01-01
    • 2013-07-28
    • 1970-01-01
    • 2014-12-18
    • 2017-10-22
    • 2017-12-24
    • 2016-08-09
    • 1970-01-01
    相关资源
    最近更新 更多