簡體   English   中英

Knex JavaScript函數未按正確的順序執行

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

我目前正在學習如何使用Knex,並且無法以正確的順序運行編寫的功能。

運行代碼時,我期望發生的事情是刪除表(如果存在),創建表,最后為表填充數據。 現在,種子函數正在表創建之前運行,這會導致程序崩潰。

它應按以下順序在index.js中運行: main() > dropTables() > createTables() > seedTables()

如何使這些功能以正確的順序運行?

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)
    })
}

除了在箭頭函數中調用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