简体   繁体   English

Knex JavaScript函数未按正确的顺序执行

[英]Knex JavaScript functions not executing in the correct order with await

I'm currently learning how to use Knex and I'm having trouble having the functions I've written run in the correct order. 我目前正在学习如何使用Knex,并且无法以正确的顺序运行编写的功能。

When the code is run, what I'm expecting to happen is the tables are dropped (if they exist), the tables are created, and finally the tables are seeded with data. 运行代码时,我期望发生的事情是删除表(如果存在),创建表,最后为表填充数据。 Right now the seeding function is running before the table creation which causes the program to crash. 现在,种子函数正在表创建之前运行,这会导致程序崩溃。

It should run in the following order in index.js: main() > dropTables() > createTables() > seedTables() . 它应按以下顺序在index.js中运行: main() > dropTables() > createTables() > seedTables()

How do I get these functions to run in the correct order? 如何使这些功能以正确的顺序运行?

index.js

require("babel-core/register")
require("babel-polyfill")

import { makeUserTable, dropUserTable } from './models/users'
import { makeGameTable, dropGameTable } from './models/games'
import { seedUser } from './seeds/users'
import { seedGame } from './seeds/games'

const dbConnection = require('knex')({
  client: 'mysql',
  debug: false,
  connection: {
    host: 'localhost',
    user: 'samurai',
    password: 'bushido',
    database: 'knex_sandbox'
  }
})

function dropTables() {
  dropUserTable(dbConnection)
  dropGameTable(dbConnection)
}

function makeTables() {
  makeUserTable(dbConnection)
  makeGameTable(dbConnection)
}

function seedTables() {
  // seedUser(dbConnection)
  // seedGame(dbConnection)
  console.log('seed tables ran')
}

const main = () => {
  try {
    dropTables()
    makeTables()
    seedTables()

    // kill program
    dbConnection.destroy()
    console.log('===> Script done. Exiting.')
  } catch (err) {
    console.log('===> Something went wrong:\n', err)
  }
}

export default main(dbConnection)

models/users.js

export const makeUserTable = (dbConnection) => {
  dbConnection.schema
    .createTableIfNotExists('users', function (table) {
      table.increments('user_id').primary()
      table.string('username', 100)
      table.text('bio', 100)
    })
    .then(() => {
      console.log('===> User table created.')
    })
    .catch(err => {
      console.log('===> Something went wrong creating the user table:\n', err)
    })
}

export const dropUserTable = (dbConnection) => {
  dbConnection.schema
    .dropTableIfExists('users')
    .then(() => {
      console.log('===> User table dropped (if it exists).')
    })
    .catch(err => {
      console.log('===> Something went wrong dropping the user table:\n', err)
    })
}

models/games.js

export const makeGameTable = (dbConnection) => {
  dbConnection.schema
    .createTableIfNotExists('games', function (table) {
      table.increments('game_id')
        .primary()
      table.string('title', 100)
      table.text('description', 100)
      // table.integer('user_id')
      //   .unsigned()
      //   .references('users.user_id')
      //   .onDelete('cascade')
    })
    .then(() => {
      console.log('===> Game table created.')
    })
    .catch(err => {
      console.log('===> Something went wrong creating the game table:\n', err)
    })
}

export const dropGameTable = (dbConnection) => {
  dbConnection.schema
    .dropTableIfExists('games')
    .then(() => {
      console.log('===> Game table dropped (if it exists).')
    })
    .catch(err => {
      console.log('===> Something went wrong dropping the game table:\n', err)
    })
}

seeds/users.js

export const seedUser = (dbConnection) => {
  dbConnection
    .insert({
      username: 'dog',
      bio: 'Hello, this is dog'
    })
    .into('users')
    .then(() => {
      console.log('===> User seeded')
    })
    .catch(err => {
      console.log('===> Something went wrong seeding the user table:\n', err)
    })
}

seeds/games.js

export const seedGame = (dbConnection) => {
  dbConnection
    .insert({
      title: 'Bone',
      description: 'Om nom nom',
      // user_id: 1
    })
    .into('games')
    .then(() => {
      console.log('===> Game seeded')
    })
    .catch(err => {
      console.log('===> Something went wrong seeding the game table:\n', err)
    })
}

Instead of just calling dbConnection... in your arrow functions you should also return the created promise lie this return dbConnection... and put awaits back before calling them. 除了在箭头函数中调用dbConnection... ,还应该返回创建的promise所在,此return dbConnection...并在调用它们之前将其等待。

require("babel-core/register")
require("babel-polyfill")

import { makeUserTable, dropUserTable } from './models/users'
import { makeGameTable, dropGameTable } from './models/games'
import { seedUser } from './seeds/users'
import { seedGame } from './seeds/games'

const dbConnection = require('knex')({
  client: 'mysql',
  debug: false,
  connection: {
    host: 'localhost',
    user: 'samurai',
    password: 'bushido',
    database: 'knex_sandbox'
  }
})

async function dropTables() {
  await dropUserTable(dbConnection)
  await dropGameTable(dbConnection)
}

async function makeTables() {
  await makeUserTable(dbConnection)
  await makeGameTable(dbConnection)
}

async function seedTables() {
  // seedUser(dbConnection)
  // seedGame(dbConnection)
  console.log('seed tables ran')
}

const main = async () => {
  try {
    await dropTables()
    await makeTables()
    await seedTables()

    // kill program
    await dbConnection.destroy()
    console.log('===> Script done. Exiting.')
  } catch (err) {
    console.log('===> Something went wrong:\n', err)
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM