我正在使用 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/
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser
我刚刚被困在这个问题上一段时间了。以这个基地为例:moduleTopclassTestendmoduleFooendend稍后,我可以通过这样做在Foo中定义扩展Test的类:moduleTopmoduleFooclassSomeTest但是,如果我尝试通过使用::指定模块来最小化缩进:moduleTop::FooclassFailure这失败了:NameError:uninitializedconstantTop::Foo::Test这是一个错误,还是仅仅是Ruby解析变量名的方式的逻辑结果? 最佳答案 Isthisabug,or
我正在阅读SandiMetz的POODR,并且遇到了一个我不太了解的编码原则。这是代码:classBicycleattr_reader:size,:chain,:tire_sizedefinitialize(args={})@size=args[:size]||1@chain=args[:chain]||2@tire_size=args[:tire_size]||3post_initialize(args)endendclassMountainBike此代码将为其各自的属性输出1,2,3,4,5。我不明白的是查找方法。当一辆山地自行车被实例化时,因为它没有自己的initialize方法
我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c
我的假设是moduleAmoduleBendend和moduleA::Bend是一样的。我能够从thisblog找到解决方案,thisSOthread和andthisSOthread.为什么以及什么时候应该更喜欢紧凑语法A::B而不是另一个,因为它显然有一个缺点?我有一种直觉,它可能与性能有关,因为在更多命名空间中查找常量需要更多计算。但是我无法通过对普通类进行基准测试来验证这一点。 最佳答案 这两种写作方法经常被混淆。首先要说的是,据我所知,没有可衡量的性能差异。(在下面的书面示例中不断查找)最明显的区别,可能也是最著名的,是你的
我一直致力于让我们的Rails2.3.8应用程序在JRuby下正确运行。一切正常,直到我启用config.threadsafe!以实现JRuby提供的并发性。这导致lib/中的模块和类不再自动加载。使用config.threadsafe!启用:$rubyscript/runner-eproduction'pSim::Sim200Provisioner'/Users/amchale/.rvm/gems/jruby-1.5.1@web-services/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in`co
我有一个Controller,我想为这个Controller创建一个助手,我可以在不包含它的情况下使用它。我尝试像这样创建一个与Controller同名的助手classCars::EnginesController我创建的助手是moduleCars::EnginesHelperdefcheck_fuellogger.debug("chekingfuel")endend我得到的错误是undefinedlocalvariableormethod`check_fuel'for#有没有我遗漏的约定? 最佳答案 如果你真的想在Controll