我想在我的package.json脚本中使用js文件创建用户,但是当我通过npm run create_admin
运行脚本时,不会创建用户并且不会抛出任何错误。一些快速调试让我相信Account.register
没有被调用。
package.json脚本 -
"scripts": {
"start": "node bin/www",
"test": "mocha --recursive",
"create_admin": "node scripts/create_admin"}
create_admin.js -
var prompt = require('prompt'),
Account = require('../models/account'),
util = require('../utility/utility');
var schema = {
properties: {
username: {
pattern: /^[a-zA-Z\s\-]+$/,
message: 'Username must contain only letters, spaces, or dashes.',
required: true,
description: 'Enter username'
},
password: {
hidden: true,
required: true,
description: 'Enter password'
},
confirmPassword: {
hidden: true,
required: true,
description: 'Confirm password'
}
}
};
prompt.start();
prompt.get(schema, function(err, result) {
if (result.password === result.confirmPassword) {
var newAccount = {
username: result.username,
isAdmin: true,
regDate: Date.now(),
lastLogin: Date.now()
};
Account.register(new Account(newAccount), result.password, function(err, account) {
if (err) {
console.log('Username already in use. Please try another username');
} else {
console.log('Admin account created');
}
});
} else {
console.log('Error - passwords do not match');
}
});
感谢任何帮助。
答案 0 :(得分:0)
将我的头撞在墙上一段时间后试图解决这个问题,我发现我忽略了require
我的应用程序。添加修复问题并调用Account.register
。
已更新为包含应用
var app = require('../app'),
prompt = require('prompt'),
Account = require('../models/account'),
util = require('../utility/utility');
var schema = {
properties: {
username: {
pattern: /^[a-zA-Z\s\-]+$/,
message: 'Username must contain only letters, spaces, or dashes.',
required: true,
description: 'Enter username'
},
password: {
hidden: true,
required: true,
description: 'Enter password'
},
confirmPassword: {
hidden: true,
required: true,
description: 'Confirm password'
}
}
};
prompt.start();
prompt.get(schema, function(err, result) {
if (result.password === result.confirmPassword) {
var newAccount = {
username: result.username,
isAdmin: true,
regDate: Date.now(),
lastLogin: Date.now()
};
Account.register(new Account(newAccount), result.password, function(err, account) {
if (err) {
console.log('Username already in use. Please try another username');
} else {
console.log('Admin account created');
}
});
} else {
console.log('Error - passwords do not match');
}
});