1

我有一个要通过 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');'

我无法弄清楚我在这里做错了什么。希望有人可以帮助我并解释为什么会这样

4

1 回答 1

3

您忘记node在命令中指定,所以我怀疑 Heroku 试图createUser.js像 shell 脚本一样运行。

您可能需要安装 node.js buildpack 才能在 Heroku 上运行该程序,但请尝试:

heroku run node bin/createUser.js --username admin --password admin --clientId -1
于 2017-02-28T23:01:32.563 回答