javascript - Meteor、NPM 模块和光纤回调

标签 javascript node.js mongodb meteor npm

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

最佳答案

改用这个包: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);
}

关于javascript - Meteor、NPM 模块和光纤回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30593640/

相关文章:

mongodb - 如何将对象推送到 mongodb 数组中

javascript - 谷歌关闭优化

javascript - 为面添加厚度

javascript - Node.js 异常处理最佳实践 - Async/Await 之后

node.js - ffmpeg - ffmpeg aws lambda 函数错误

javascript - jsdom获取没有图像的文本

javascript - 如何从同一 Controller 的另一个函数调用 Sails.js Controller 的函数?

javascript - mongodb - 展开嵌套子文档

mongodb - 根据两个值查找元素

javascript - 在 d3.scaleTime() 中获取恒定的刻度间隔值