2016-04-29 170 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'似乎是一個字符串。 –