【问题标题】:Meteor insert / remove operations raise exception when called from ZeroMQ callbacks从 ZeroMQ 回调调用 Meteor 插入/删除操作时引发异常
【发布时间】:2013-08-28 08:18:23
【问题描述】:

当在指定端口上接收到 ZeroMQ 消息时,以下 Meteor 代码 sn -p barfs。但是,如果我将remove 更改为find,它可以正常工作。 insert 操作也会失败。请注意,如果在回调之外运行,insertremove 会成功,例如就在pull.on 部分的下方。

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)

有人有什么想法吗?谢谢!

【问题讨论】:

    标签: javascript node.js mongodb meteor zeromq


    【解决方案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);
    
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-08
      • 2016-01-24
      • 2015-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多