I use the Meteor package tomi:upload-jquery that uses callbacks outside a fiber. Inside one callback I should use MongoDB, and MongoDB requires to be run inside a fiber.
After a lot of attempts with future, fibers, deasync, Meteor.bindEnvironment, Meteor.wrapAsync, a.s.o. I have found no solution.
The callback to be wrappen is:
Meteor.startup ->
UploadServer.init
getDirectory: (fileInfo, formData) ->
# begin some sort of wrapping
dir = ''
u = Meteor.users.findOne({"uploadToken.token": formData.uploadToken })
if u
dir = u.username
else
throw new Error 'Anonymous calls not permitted'
# end some sort of wrapping
console.log 'dir', dir
return dir
return
EDIT 1
This code is working without crashing, but it should become synchronous:
Meteor.startup ->
UploadServer.init
getDirectory: (fileInfo, formData) ->
getDir = (formData,callback) ->
Fiber(()->
console.log '0:',formData
dir = ''
u = Meteor.users.findOne({"uploadToken.token": formData.uploadToken })
if u
dir = u.username
else
callback new Error( 'Anonymous file upload not permitted.' ), null
console.log '1:',dir
callback null,dir
).run()
dir = getDir(formData, (e,r)->
console.log 'e,r:',e,r
)
console.log '2:',dir
return ''
return
In the log I get:
I20150905-08:33:13.171(2)? 0: { uploadToken: 'K2eEiFMRMagSuoKqf' }
I20150905-08:33:13.173(2)? 2: undefined
I20150905-08:33:13.174(2)? 0: { uploadToken: 'K2eEiFMRMagSuoKqf' }
I20150905-08:33:13.175(2)? 2: undefined
I20150905-08:33:13.177(2)? 1: test
I20150905-08:33:13.177(2)? e,r: null test
I20150905-08:33:13.177(2)? 1: test
I20150905-08:33:13.178(2)? e,r: null test
getDirectory is called twice.
What is missing now is how to make to code synchronous. That is to let return dir
wait for getDir
.
Any advice and suggestions will be greatly appreciated.