[英]How can I unregister a Collection in Meteor?
我正在尝试实现简单的功能,以允许Meteor应用程序检查某个MongoDB字符串是否可以成功用于连接。 为了做到这一点,我有一个事件触发一个服务器方法,该方法建立与MongoDB的连接并返回集合中的文档数。 我正在使用的(简化)方法采用mongo连接字符串和集合名称,并返回集合内的文档数。
var mongoUrl = "mongodb://user:password@192.168.0.98:27017/mydb";
var collectionName = "test";
Meteor.methods({
testDbConnection: function(mongoUrl, collectionName) {
var database = new MongoInternals.RemoteCollectionDriver(mongoUrl);
TestCollection = new Mongo.Collection(collectionName, { _driver: database });
var numberOfDocs = TestCollection.find().count();
// HELP: how to unregister TestCollection?
return numberOfDocs;
}
});
该方法的第一个调用有效,但是由于我尚未注销TestCollection
,因此所有后续调用都会失败,因为Meteor已经为定义的test
集合提供了insert
方法。
Exception while invoking method 'testDbConnection' Error: A method named '/test/insert' is already defined
at packages/ddp/livedata_server.js:1444:1
at Function._.each._.forEach (packages/underscore/underscore.js:113:1)
at [object Object]._.extend.methods (packages/ddp/livedata_server.js:1442:1)
at [object Object].Mongo.Collection._defineMutationMethods (packages/mongo/collection.js:886:1)
at new Mongo.Collection (packages/mongo/collection.js:208:1)
at [object Object].Meteor.methods.testDbConnection (app/app.js:55:24)
at maybeAuditArgumentChecks (packages/ddp/livedata_server.js:1599:1)
at packages/ddp/livedata_server.js:648:1
at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
at packages/ddp/livedata_server.js:647:1
我如何在Meteor中注销集合以执行这些临时数据库连接?
尽管我没有直接回答您的问题,但我有一个想法可能对您的追求有所帮助!
我会考虑使用“工作者”方法,这是您的Meteor应用程序,它执行单个任务并死亡。 这样您就不必担心清理了。
如果您不使用Velocity,您仍然可以使用上面的相同方法,方法是从主应用中派生child_process来启动.meteor/.../build/main.js
您可以通过将child_process传递给环境变量来告诉您的应用程序处于工作模式。
旁注:自从我们(速度小组)对后视镜进行此操作以来,我一直在考虑编写完成上述任务的Meteor Worker程序包。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.