我意识到已经讨论过类似的主题,我浏览了所有这些主题并尝试模仿解决方案大约 4 小时,但似乎没有一个能解决问题。我不会在用尽我能找到的任何潜在解决方案的情况下发布这个。
我正在尝试将大型 csv 转换为 JSON,然后在转换后将其插入到我的 Meteor 集合中。
这是我使用 Meteor.bindElement 方法的代码:
if Meteor.isServer
@Fiber = Meteor.npmRequire("fibers")
Meteor.startup ->
#Converter Class
if Data.find().count() is 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 will be emitted once parsing finished
csvConverter.on "end_parsed", (jsonObj) ->
console.log jsonObj
boundFunction = Meteor.bindEnvironment(->
Data.insert jsonObj
return
, (e) ->
throw e
return
)
insertToDB boundFunction
#read from file
fileStream.pipe csvConverter
return
解析后,我可以很好地记录 JSON,但是当我尝试像这样将它添加到集合中时:
csvConverter.on "end_parsed", (jsonObj) ->
console.log jsonObj
Data.insert jsonObj
我得到错误:
throw new Error("Meteor code must always run within a Fiber. " +
W20141112-20:08:44.306(-8)? (STDERR) ^
W20141112-20:08:44.306(-8)? (STDERR) Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.
W20141112-20:08:44.306(-8)? (STDERR) at Object.Meteor._nodeCodeMustBeInFiber (packages/meteor/dynamics_nodejs.js:9)
W20141112-20:08:44.306(-8)? (STDERR) at _.extend.get (packages/meteor/dynamics_nodejs.js:21)
W20141112-20:08:44.306(-8)? (STDERR) at Object.DDP.randomStream (packages/ddp/random_stream.js:69)
W20141112-20:08:44.306(-8)? (STDERR) at self._makeNewID (packages/mongo/collection.js:72)
W20141112-20:08:44.306(-8)? (STDERR) at Mongo.Collection.(anonymous function) [as insert] (packages/mongo/collection.js:485)
W20141112-20:08:44.307(-8)? (STDERR) at csvAdv.<anonymous> (server/startup.coffee:16:13)
W20141112-20:08:44.307(-8)? (STDERR) at csvAdv.emit (events.js:95:17)
W20141112-20:08:44.307(-8)? (STDERR) at 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) at csvAdv.emit (events.js:117:20)
W20141112-20:08:44.307(-8)? (STDERR) at _stream_readable.js:929:16
=> Exited with code: 8
我可以通过使用以下代码将函数包装在纤维中来摆脱它,但它不会写入我的集合:
if Meteor.isServer
@Fiber = Meteor.npmRequire("fibers")
Meteor.startup ->
#Converter Class
if Data.find().count() is 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 will be emitted once parsing finished
csvConverter.on "end_parsed", (jsonObj) ->
Fiber ->
Data.insert jsonObj
return
fileStream.pipe csvConverter
return
因此,我尝试使用第一块代码中所示的 bindEnvironment,这会导致相同的“错误(“Meteor 代码必须始终在 Fiber 内运行”错误。
这是我 /lib 文件夹中的收藏:
@Data = new Mongo.Collection 'data'
我对 Meteor Javascript 和 Coffescript 还很陌生,所以任何反馈都将不胜感激。
另外我想补充一点,我无法让 .run() 在 Fiber 上工作。