【问题标题】:Meteor, NPM Modules, and Fiber CallbackMeteor、NPM 模块和 Fiber 回调
【发布时间】:2015-08-16 02:26:12
【问题描述】:

我正在使用 request 和 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

【问题讨论】:

    标签: javascript node.js mongodb meteor npm


    【解决方案1】:

    改用这个包:https://atmospherejs.com/froatsnook/request

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

    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);
    }
    

    【讨论】:

    • 查看编辑过的问题,如果我使用这个会出错。
    【解决方案2】:

    Meteor.bindEnvironment 使用的正确格式

    request({
           uri: 'http://yahoo.com',
           method: 'HEAD' // or GET
    
          },
          Meteor.bindEnvironment(function(err, res, body) {
    // do something here
           })
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-15
      • 1970-01-01
      • 2018-05-26
      • 2016-07-22
      • 2019-10-21
      • 2018-07-05
      • 2015-06-06
      • 2015-12-09
      相关资源
      最近更新 更多