问题
I have a Meteor app. that uses a database different form the default 'meteor' one:
var database = new MongoInternals.RemoteCollectionDriver("mongodb://127.0.0.1:3001/my-db");
However, every time I register a new user, using the Accouts-Base package, it keeps adding the new user record to the Users collection in the 'meteor' database. Is there any way to use this package with an alternate database?
P.s. I am in development environment if that makes any difference.
回答1:
Looking directly at the code for the accounts-base package (Meteor v 1.0.4), it looks like they do not officially support a way to set the database for the users collection. As you can see from the code, the server always connects using the default Meteor.connection:
Meteor.users = new Mongo.Collection("users", { // line 141
_preventAutopublish: true,
connection: Meteor.isClient ? Accounts.connection : Meteor.connection
});
The Accounts.connection is set above, but it is explicitly not supported:
// ~ 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)
}
}
However, I was able to get it to use my database by setting the $MONGO_URL environment variable (which I believe sets the default connection, which gets used by the accounts package):
In one terminal window, I started mongo on port 27017
> mongod
In another window, I set the MONGO_URL and added the appropriate packages, then started meteor:
> export MONGO_URL=mongodb://localhost:27017/test
> meteor add accounts-base
> meteor add accounts-password
> meteor
And finally in my browser console I created an account:
> Accounts.createUser({username: 'me', password: 'guest'});
Then I connected to test database in my mongo instance:
> mongo
MongoDB shell version: 3.0.1
connecting to: test
> db.users.find()
{ "_id" : "L3EDrS8FnRymDLhPp", ... "username" : "me" }
In short, I think you have three not-super-great options:
- Use the
MONGO_URLenvironment variable (probably best option) - Hack the accounts-base package to do what you want
- Try out the unsupported
ACCOUNTS_CONNECTION_URLvariable, which may 'go away at any point in time'
来源:https://stackoverflow.com/questions/29622726/cannot-get-accounts-base-package-to-point-to-the-correct-database