对于普通集合(即不同于Meteor.users),您可以直接访问您的 MongoDB 集合。当您的项目在开发模式下运行时打开 Meteor Mongo shell,然后直接键入 Mongo shell 命令。
对于Meteor.users 集合,您希望利用accounts-base 和accounts-password 包自动管理,因此您希望通过Meteor 应用程序插入文档/用户,而不是直接摆弄MongoDB。
很遗憾,您的应用源文件(如您的 UsersFixtures.js 文件)绝对不适合 CLI 使用。
通常的解决方案是在您的应用服务器中嵌入一个专用方法:
// On your server.
// Make sure this Method is not available on production.
// When started with `meteor run`, NODE_ENV will be `development` unless set otherwise previously in your environment variables.
if (process.env.NODE_ENV !== 'production') {
Meteor.methods({
addTestUser(username, password) {
Accounts.createUser({
username,
password // If you do not want to transmit the clear password even in dev environment, you can call the method with 2nd arg: {algorithm: "sha-256", digest: sha256function(password)}
})
}
});
}
然后在开发模式下启动你的 Meteor 项目 (meteor run),在浏览器中访问你的应用,打开浏览器控制台,然后直接从那里调用方法:
Meteor.call('addTestUser', myUsername, myPassword)
您也可以直接在浏览器控制台中使用Accounts.createUser,但它会自动让您以新用户身份登录。