关于 javascript:如何在 Meteor 中取消注册收藏? | 珊瑚贝

How can I unregister a Collection in Meteor?


我正在尝试实现简单的功能,以允许 Meteor 应用程序检查某个 MongoDB 字符串是否可以成功用于连接。为了做到这一点,我有一个事件触发一个服务器方法,该方法建立与 MongoDB 的连接并返回集合内的文档数。
我正在使用的(简化)方法采用 mongo 连接字符串和集合名称,并返回集合中的文档数。

1
2
3
4
5
6
7
8
9
10
11
12
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 方法。

1
2
3
4
5
6
7
8
9
10
11
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 中取消注册集合以执行这些临时数据库连接?

  • 由于创建 Collection 不仅仅是为 mongo 集合提供接口,那么对于那些简单的即席查询,您最好使用 MongoInternals 或 npm mongodb 吗?
  • 事实上,使用 var numberOfDocs = database.open(collectionName).find().count(); 确实可以很好地工作,但对我来说问题仍然存在。


虽然我没有回答您的直接问题,但我有一个想法可能对您的探索有所帮助!

我会考虑使用”工作人员”方法,这是您的 Meteor 应用程序,它执行单个任务并死掉。那你就不用担心清理了。

  • 如果使用 Velocity,你可以启动一个”自毁镜像”。你可以像这样启动它并像这样杀死镜像。

  • 如果您不使用 Velocity,您仍然可以使用上述相同的方法,方法是从您的主应用中分叉一个 child_process 来启动 .meteor/…/build/main.js

您可以通过向 child_process 传递一个环境变量来告诉您的应用程序处于工作模式。

旁注:自从我们(velocity 团队)为镜像做这件事以来,我一直在考虑编写一个 Meteor 工作包来完成上述工作。


来源:https://www.codenong.com/28383589/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9194.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?