Meteor,NPM模块和光纤回调

时间:2015-06-02 10:33:46

标签: javascript node.js mongodb meteor npm

我正在使用请求和cheerio来解析网站并从中获取特定内容,技术上是一个引用。

这是我的代码(server.js):

 q = new Mongo.Collection('quotelist');
  postList = new Mongo.Collection('quotes');
  fruits = [];
  var a = "";

  var cheerio = Meteor.npmRequire('cheerio');
  var request = Meteor.npmRequire('request');


  request('http://www.goodreads.com/author/quotes/1406384.John_Green', Meteor.bindEnvironment(function() {
          for (i = 0; i < fruits.length; i++) {
              postList.insert({
                  quote: fruits[i]
              });
              console.log(fruits[10]);
          }

      }),
      function(err, resp, body) {
          if (!err && resp.statusCode == 200) {
              $ = cheerio.load(body);
              a = $('.quoteText', '.quote').html();


              $('.quoteText').each(function(i, elem) {
                  fruits[i] = $(this).text();
              });
              console.log(fruits[10]);

          }


      });

但是我收到以下错误:

    W20150602-15:57:50.046(5.5)? (STDERR)
    W20150602-15:57:50.047(5.5)? (STDERR) Error: Meteor code must always run within
    a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteo
    r.bindEnvironment.
    W20150602-15:57:50.047(5.5)? (STDERR)     at Object.Meteor._nodeCodeMustBeInFibe
    r (packages/meteor/dynamics_nodejs.js:9:1)
    W20150602-15:57:50.048(5.5)? (STDERR)     at Object.Meteor.bindEnvironment (pack
    ages/meteor/dynamics_nodejs.js:85:1)
    W20150602-15:57:50.048(5.5)? (STDERR)     at Request._callback (app\server\app.j
    s:22:24)
    W20150602-15:57:50.049(5.5)? (STDERR)     at Request.self.callback (C:\Users\Abh
    ishek\new\.meteor\local\isopacks\npm-container\npm\node_modules\request\request.
    js:354:22)
    W20150602-15:57:50.050(5.5)? (STDERR)     at Request.<anonymous> (C:\Users\Abhis
    hek\new\.meteor\local\isopacks\npm-container\npm\node_modules\request\request.js
    :1207:14)
    W20150602-15:57:50.050(5.5)? (STDERR)     at Request.emit (events.js:117:20)
    W20150602-15:57:50.051(5.5)? (STDERR)     at IncomingMessage.<anonymous> (C:\Use
    rs\Abhishek\new\.meteor\local\isopacks\npm-container\npm\node_modules\request\re
    quest.js:1153:12)
    W20150602-15:57:50.050(5.5)? (STDERR)     at Request.emit (events.js:98:17)
    W20150602-15:57:50.053(5.5)? (STDERR)     at IncomingMessage.emit (events.js:117
    :20)
    W20150602-15:57:50.054(5.5)? (STDERR)     at _stream_readable.js:944:16


**All I have to do, is get the data from cheerio and request and add it to a database so that I can iterate it and show it on the website `using {{each}}`**.

如果我使用Froatsnook包,     错误是:

I20150602-16:54:49.715(5.5)? [TypeError: Object function request(uri, options, c
allback) {
I20150602-16:54:49.716(5.5)?   if (typeof uri === 'undefined') {
I20150602-16:54:49.716(5.5)?     throw new Error('undefined is not a valid uri o
r options object.')
I20150602-16:54:49.716(5.5)?   }
I20150602-16:54:49.717(5.5)?
I20150602-16:54:49.717(5.5)?   var params = initParams(uri, options, callback)
I20150602-16:54:49.718(5.5)?
I20150602-16:54:49.718(5.5)?   if (params.method === 'HEAD' && paramsHaveRequest
Body(params)) {
I20150602-16:54:49.718(5.5)?     throw new Error('HTTP HEAD requests MUST NOT in
clude a request body.')
I20150602-16:54:49.719(5.5)?   }
I20150602-16:54:49.719(5.5)?
I20150602-16:54:49.719(5.5)?   return new request.Request(params)
I20150602-16:54:49.720(5.5)? } has no method 'getSync']
=> Meteor server restarted
I20150602-16:55:44.529(5.5)? [TypeError: Object function request(uri, options, c
allback) {
I20150602-16:55:44.530(5.5)?   if (typeof uri === 'undefined') {
I20150602-16:55:44.530(5.5)?     throw new Error('undefined is not a valid uri o
r options object.')
I20150602-16:55:44.531(5.5)?   }
I20150602-16:55:44.531(5.5)?
I20150602-16:55:44.531(5.5)?   var params = initParams(uri, options, callback)
I20150602-16:55:44.532(5.5)?
I20150602-16:55:44.532(5.5)?   if (params.method === 'HEAD' && paramsHaveRequest
Body(params)) {
I20150602-16:55:44.533(5.5)?     throw new Error('HTTP HEAD requests MUST NOT in
clude a request body.')
I20150602-16:55:44.533(5.5)?   }
I20150602-16:55:44.533(5.5)?
I20150602-16:55:44.534(5.5)?   return new request.Request(params)
I20150602-16:55:44.534(5.5)? } has no method 'getSync']
=> Meteor server restarted

2 个答案:

答案 0 :(得分:1)

请改用此套餐:https://atmospherejs.com/froatsnook/request

它提供了npm请求包的同步版本,因此您不必处理Fiber问题。

try{
  var response = request.getSync({
    url: "http://www.goodreads.com/author/quotes/1406384.John_Green"
  });
  if(response.statusCode == 200){
    console.log(response.body);
  }
}
catch(exception){
  console.log(exception);
}

答案 1 :(得分:0)

Meteor.bindEnvironment使用的正确格式

request({
       uri: 'http://yahoo.com',
       method: 'HEAD' // or GET

      },
      Meteor.bindEnvironment(function(err, res, body) {
// do something here
       })

或者你可以使用saimeunt提到的request.getSync。