我有一个脚本,我想通过Heroku CLI运行。这只是一个使用Sequelize在postgressql数据库中创建用户的简单脚本。 这是脚本:
const argv = require('yargs').argv;
const Sequelize = require('sequelize');
const sqlizr = require('sqlizr');
require('dotenv').load();
// Check params
if (!argv.username) { throw new Error('username is required!'); }
if (!argv.password) { throw new Error('password is required!'); }
if (!argv.clientId) { throw new Error('client id is required!'); }
// Init db connection
const sequelize = new Sequelize(
process.env.DB_DATABASE,
process.env.DB_USER,
process.env.DB_PASS,
{
host: process.env.DB_HOST,
port: 5432,
dialect: 'postgres',
logging: false
}
)
var client_id = argv.clientId;
if(argv.clientId === -1){
client_id = 0;
}
console.log(sequelize)
sqlizr(sequelize, 'api/models/**/*.js');
// Check if user exists
console.log('Check is user exists...');
sequelize.models.USERS.count({
where: {
USERNAME: argv.username
}
})
.then(result => {
if (result > 0) {
console.error('user already exists!');
process.exit(1);
}
})
.then(() => {
console.log('Creating user...');
sequelize.models.USERS.create({
USERNAME: argv.username,
PASSWORD: argv.password,
CLNT_ID: client_id,
EMAIL: 'email@email.com',
PHONE: '123456789'
})
.then(result => {
console.log('User created successfully!');
})
.catch(error => {
console.error('Could not create user!', error);
})
.finally(result => {
process.exit(1);
});
});
如果我在本地执行此命令,一切顺利:
node bin/createUser.js --username admin --password admin --clientId -1
但是,如果我尝试通过Heroku CLI运行这样:
heroku run bin/createUser.js --username admin --password admin --clientId -1
我在终端得到这个:
bin/createUser.js: line 4: syntax error near unexpected token `('
bin/createUser.js: line 4: `const yargs = require('yargs');'
我无法弄清楚我在这里做错了什么。希望有人可以帮助我解释为什么会发生这种情况
答案 0 :(得分:2)
您忘记在命令中指定node
,因此我怀疑Heroku正在尝试运行createUser.js
,就像它是shell脚本一样。
您可能需要安装node.js buildpack才能在Heroku上运行该程序,但请尝试:
heroku run node bin/createUser.js --username admin --password admin --clientId -1