coffeescript - JSON will not insert into Meteor Collection (even with Fiber or bindEnvironment) -
coffeescript - JSON will not insert into Meteor Collection (even with Fiber or bindEnvironment) -
i realize similar topics have been discussed, have browsed through them , tried mimic solutions 4 hours none of them seem solve problem. not have posted having exhausted potential solution find.
i trying convert big csv json , insert meteor collection next conversion.
here code using meteor.bindelement approach:
if meteor.isserver @fiber = meteor.npmrequire("fibers") meteor.startup -> #converter class if data.find().count() 0 converter = meteor.npmrequire('csvtojson').core.converter fs = meteor.npmrequire('fs') csvfilename = '/path/to/my/file.csv' filestream = fs.createreadstream(csvfilename) #new converter instance csvconverter = new converter(constructresult: true) #end_parsed emitted 1 time parsing finished csvconverter.on "end_parsed", (jsonobj) -> console.log jsonobj boundfunction = meteor.bindenvironment(-> data.insert jsonobj homecoming , (e) -> throw e homecoming ) inserttodb boundfunction #read file filestream.pipe csvconverter homecoming
after parsing able log json fine when seek add together collection this:
csvconverter.on "end_parsed", (jsonobj) -> console.log jsonobj data.insert jsonobj
i error:
throw new error("meteor code must run within fiber. " + w20141112-20:08:44.306(-8)? (stderr) ^ w20141112-20:08:44.306(-8)? (stderr) error: meteor code must run within fiber. seek wrapping callbacks pass non-meteor libraries meteor.bindenvironment. w20141112-20:08:44.306(-8)? (stderr) @ object.meteor._nodecodemustbeinfiber (packages/meteor/dynamics_nodejs.js:9) w20141112-20:08:44.306(-8)? (stderr) @ _.extend.get (packages/meteor/dynamics_nodejs.js:21) w20141112-20:08:44.306(-8)? (stderr) @ object.ddp.randomstream (packages/ddp/random_stream.js:69) w20141112-20:08:44.306(-8)? (stderr) @ self._makenewid (packages/mongo/collection.js:72) w20141112-20:08:44.306(-8)? (stderr) @ mongo.collection.(anonymous function) [as insert] (packages/mongo/collection.js:485) w20141112-20:08:44.307(-8)? (stderr) @ csvadv.<anonymous> (server/startup.coffee:16:13) w20141112-20:08:44.307(-8)? (stderr) @ csvadv.emit (events.js:95:17) w20141112-20:08:44.307(-8)? (stderr) @ csvadv.<anonymous> (/users/samuel_chordas/currentclasses/csci477a/jpl/jatatwo/packages/npm-container/.build.npm-container/npm/node_modules/csvtojson/libs/core/init_onend.js:9:10) w20141112-20:08:44.307(-8)? (stderr) @ csvadv.emit (events.js:117:20) w20141112-20:08:44.307(-8)? (stderr) @ _stream_readable.js:929:16 => exited code: 8
which able rid of wrapping function in fiber next code not write collection:
if meteor.isserver @fiber = meteor.npmrequire("fibers") meteor.startup -> #converter class if data.find().count() 0 converter = meteor.npmrequire('csvtojson').core.converter fs = meteor.npmrequire('fs') csvfilename = '/path/to/my/file.csv' filestream = fs.createreadstream(csvfilename) #new converter instance csvconverter = new converter(constructresult: true) #end_parsed emitted 1 time parsing finished csvconverter.on "end_parsed", (jsonobj) -> fiber -> data.insert jsonobj homecoming filestream.pipe csvconverter homecoming
so tried resorting using bindenvironment shown in first block of code results in same "error("meteor code must run within fiber" error.
here collection in /lib folder:
@data = new mongo.collection 'data'
i pretty new meteor javascript , coffescript feedback appreciated.
also add together can't .run() work on fiber.
try moving definition of boundfunction
outside of end_parsed
event handler.
boundfunction = meteor.bindenvironment((jsonobj) -> data.insert jsonobj homecoming , (e) -> throw e ) csvconverter.on "end_parsed", (jsonobj) -> console.log jsonobj boundfunction jsonobj homecoming
i suspect "meteor code must run within fiber." error not coming data.insert
meteor.bindenvironment
itself. meteor.bindenvironment
bindenvironment
called from in implementation, time phone call bindenvironment
, "meteor context" has been lost, bindenvironment
can't help.
meteor coffeescript fiber
Comments
Post a Comment