code
code

Reputation: 2163

Meteor, NPM Modules, and Fiber Callback

I am using request and cheerio to parse a website and get specific content out of that, technically a quote.

here's my code ( 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]);

          }


      });

I however get the following error :

    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}}`**.

If I use the Froatsnook package, the error is :

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

Upvotes: 2

Views: 167

Answers (2)

ParTYZane
ParTYZane

Reputation: 231

Right format for Meteor.bindEnvironment use

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

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

Or you can use request.getSync mentioned by saimeunt.

Upvotes: 0

saimeunt
saimeunt

Reputation: 22696

Use this package instead : https://atmospherejs.com/froatsnook/request

It provides a synchronous version of the npm request package, so you won't have to deal with Fibers issues.

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

Upvotes: 1

Related Questions