【问题标题】:Cannot get Accounts-Base package to point to the correct database无法让 Accounts-Base 包指向正确的数据库
【发布时间】:2015-06-19 18:58:30
【问题描述】:

我有一个 Meteor 应用程序。使用与默认“流星”不同的数据库:

var database = new MongoInternals.RemoteCollectionDriver("mongodb://127.0.0.1:3001/my-db");

但是,每次我使用 Accouts-Base 包注册新用户时,它都会不断地将新用户记录添加到“流星”数据库中的用户集合中。有没有办法将此包与备用数据库一起使用?

附:如果这有什么不同,我在开发环境中。

【问题讨论】:

    标签: mongodb meteor


    【解决方案1】:

    查看directly at the code for the accounts-base package (Meteor v 1.0.4),看起来他们不正式支持为用户集合设置数据库的方法。从代码中可以看出,服务器始终使用默认的 Meteor.connection 进行连接:

    Meteor.users = new Mongo.Collection("users", { // line 141
      _preventAutopublish: true,
      connection: Meteor.isClient ? Accounts.connection : Meteor.connection
    });
    

    Accounts.connection 已在上面设置,但明确支持:

    // ~ line 118
    if (Meteor.isClient
    ....
    if (typeof __meteor_runtime_config__ !== "undefined" &&
      __meteor_runtime_config__.ACCOUNTS_CONNECTION_URL) { 
        // Temporary, internal hook to allow the server to point the client
        // to a different authentication server. This is for a very
        // particular use case that comes up when implementing a oauth
        // server. Unsupported and may go away at any point in time.
        //
        // We will eventually provide a general way to use account-base
        // against any DDP connection, not just one special one.
        Accounts.connection = DDP.connect(
          __meteor_runtime_config__.ACCOUNTS_CONNECTION_URL)
      }
    }
    

    但是,我可以通过设置 $MONGO_URL 环境变量(我相信它设置了默认连接,由 accounts 包使用)来让它使用我的数据库:

    在一个终端窗口中,我在端口 27017 上启动了 mongo

    > mongod
    

    在另一个窗口中,我设置了 MONGO_URL 并添加了相应的包,然后启动了流星:

    > export MONGO_URL=mongodb://localhost:27017/test
    > meteor add accounts-base
    > meteor add accounts-password
    > meteor
    

    最后我在浏览器控制台中创建了一个帐户:

    > Accounts.createUser({username: 'me', password: 'guest'});
    

    然后我在我的 mongo 实例中连接到 test 数据库:

    > mongo
      MongoDB shell version: 3.0.1
      connecting to: test
    > db.users.find()
      { "_id" : "L3EDrS8FnRymDLhPp", ... "username" : "me" }
    

    简而言之,我认为您有三个不太好的选择:

    • 使用MONGO_URL 环境变量(可能是最佳选择)
    • 破解以帐户为基础的软件包来做你想做的事
    • 试用不受支持的 ACCOUNTS_CONNECTION_URL 变量,它可能“随时消失”

    【讨论】:

    • 我最终使用了 MONGO_URL。我还发现由于某种原因我使用的是 3001 端口,所以我将其更改为 27017,一切正常。
    • @JoePrivett 很高兴它成功了!我认为 3001 是默认情况下用于 Mongo 的端口 FWIW(但似乎在文档中找不到,令人惊讶)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-24
    • 2020-06-06
    • 1970-01-01
    • 1970-01-01
    • 2017-01-26
    • 2013-11-12
    • 2018-12-15
    相关资源
    最近更新 更多