2016-04-29 172 views
0

我在命令行下执行meteor npm install --save request无法使用NPM请求包流星应用

我导入请求库在我的代码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包?

回答

1

请求包的默认导出是您正在查找的对象。改变你的import语句如下:

import request from 'request'; 

这可能是因为你需要从request低级别的功能,但是你的例子也有流星的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该工作。

+0

感谢您的回复。很明显,我误导了导入时{}的使用。关于流星的http软件包,我发现使用起来有点困难,特别是如果我的网址指向图像。 'resp.content'似乎是一个字符串。 –