2013-08-27 41 views
1

當在指定端口上收到ZeroMQ消息時,以下流星代碼片段barfs。但是,如果我將remove更改爲find,則可以正常工作。 insert操作也失敗。請注意,如果在回調之外運行,則insertremove會成功,例如就在pull.on的下方。從ZeroMQ回調中調用流星插入/刪除操作會引發異常

var Components = new Meteor.Collection("components"); 

function handle_message(msg) { 
    console.log("pull on message" + msg); 
    Components.remove(); 
} 


if (Meteor.isServer) { 
    Meteor.startup(function() { 
     var zmq = Meteor.require("zmq"); 
     var pull = zmq.socket("pull"); 
     pull.bind("tcp://127.0.0.1:7000", function(data) { 
      console.log("Connection received from ZMQ"); 
     }); 
     pull.on('message', handle_message); 

    }); 
} 

的例外是:

W20130827-21:36:21.800(0)? (STDERR) packages/mongo-livedata.js:1640 
W20130827-21:36:21.802(0)? (STDERR)   throw e;                
W20130827-21:36:21.803(0)? (STDERR)    ^
W20130827-21:36:21.843(0)? (STDERR) TypeError: Cannot read property '_meteor_dynamics' of undefined 
W20130827-21:36:21.843(0)? (STDERR)  at Object.Meteor.bindEnvironment (packages/meteor/dynamics_nodejs.js:55) 
W20130827-21:36:21.845(0)? (STDERR)  at _.extend._wrapAsync (packages/meteor/helpers.js:108) 
W20130827-21:36:21.845(0)? (STDERR)  at _.each.MongoConnection.(anonymous function) [as remove] (packages/mongo-livedata/mongo_driver.js:340) 
W20130827-21:36:21.846(0)? (STDERR)  at _.each.Meteor.Collection.(anonymous function) [as remove] (packages/mongo-livedata/collection.js:406) 
W20130827-21:36:21.846(0)? (STDERR)  at Socket.handle_message (app/web.js:5:16) 
W20130827-21:36:21.847(0)? (STDERR)  at Socket.EventEmitter.emit (events.js:96:17) 
W20130827-21:36:21.847(0)? (STDERR)  at Socket._flush._flushing (/home/vagrant/.meteorite/packages/npm/arunoda/meteor-npm/ad83acff83385d5ea05997c8bbc2d7493ba4c04e/.build/npm/node_modules/zmq/lib/index.js:358:25) 
W20130827-21:36:21.852(0)? (STDERR)  at global.setImmediate (/home/vagrant/.meteorite/packages/npm/arunoda/meteor-npm/ad83acff83385d5ea05997c8bbc2d7493ba4c04e/.build/npm/node_modules/zmq/node_modules/set-immediate/setImmediate.js:15:9) 
W20130827-21:36:21.852(0)? (STDERR)  at process.startup.processNextTick.process._tickCallback (node.js:245:9) 

任何一個有什麼想法?謝謝!

回答

1

我發現了一個暗示here解決了這個問題(雖然我沒有聲稱完全理解它)。在Meteor.bindEnvironment中包裝回調消除了錯誤,並正確處理removeinsert。作爲一個流星新手,我會高興地接受另一個答案,它爲什麼/如何工作提供了一個很好的解釋。

var Components = new Meteor.Collection("components"); 

function handle_message(msg) { 
    console.log("pull on message" + msg); 
    Components.insert({"name": "foo", "state": "stopped"}); 
} 

bound_handle_message = Meteor.bindEnvironment(handle_message, function(e) { 
    console.log("exception! " + e); 
}); 

if (Meteor.isServer) { 
    Meteor.startup(function() { 
     var zmq = Meteor.require("zmq"); 
     var pull = zmq.socket("pull"); 
     pull.bind("tcp://127.0.0.1:7000", function(data) { 
      console.log("Connection received from ZMQ"); 
     }); 
     pull.on('message', bound_handle_message); 

    }); 
} 
相關問題