【问题标题】:Running script from Heroku CLI从 Heroku CLI 运行脚本
【发布时间】:2017-07-19 11:50:10
【问题描述】:

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

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

【问题讨论】:

    标签: javascript heroku sequelize.js yargs heroku-cli


    【解决方案1】:

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

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

    heroku run node bin/createUser.js --username admin --password admin --clientId -1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-02
      • 2013-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多