admin管理员组

文章数量:1336633

I've been trying to find out how to make sequelize work with 'async' and 'await'. The best information I could find on that topic was an answer in this thread: Node.js 7 how to use sequelize transaction with async / await?

But I can't quite make it work in my project. I've been cutting out parts of code to make it simpler so I can work out what exactly is not right and ended up with something like this:

const Sequelize     =   require('sequelize');
const sequelize = new Sequelize('zas', 'zas', 'saz123', 
{
    host: 'someHost',
    dialect: 'mysql',

}
);
//test
let transaction;    
var SimpleInspectionModel   = require('../models/simpleInspectionModel.js')(sequelize, { dataTypes: Sequelize.DataTypes } );

try {
  // get transaction
  transaction = await sequelize.transaction();

  // step 2
  await SimpleInspectionModel.find({}, {transaction});

  // mit
  await transactionmit();

} catch (err) {
  // Rollback transaction if any errors were encountered
  await transaction.rollback();
}

Whenever run, this code will output this kind of error :

transaction = await sequelize.transaction(); ^^^^^

SyntaxError: await is only valid in async function

at createScript (vm.js:80:10) at Object.runInThisContext (vm.js:139:10) at Module._pile (module.js:616:28) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12) at Function.Module._load (module.js:497:3) at Function.Module.runMain (module.js:693:10) at startup (bootstrap_node.js:191:16) at bootstrap_node.js:612:3

Project dependencies in package.json:

"body-parser": "^1.18.3" "express": "^4.16.3", "express-session": "^1.15.6", "file-system": "^2.2.2", "mysql2": "^1.5.3", "sequelize": "^4.37.10"

Node v8.11.3

I've been trying to find out how to make sequelize work with 'async' and 'await'. The best information I could find on that topic was an answer in this thread: Node.js 7 how to use sequelize transaction with async / await?

But I can't quite make it work in my project. I've been cutting out parts of code to make it simpler so I can work out what exactly is not right and ended up with something like this:

const Sequelize     =   require('sequelize');
const sequelize = new Sequelize('zas', 'zas', 'saz123', 
{
    host: 'someHost',
    dialect: 'mysql',

}
);
//test
let transaction;    
var SimpleInspectionModel   = require('../models/simpleInspectionModel.js')(sequelize, { dataTypes: Sequelize.DataTypes } );

try {
  // get transaction
  transaction = await sequelize.transaction();

  // step 2
  await SimpleInspectionModel.find({}, {transaction});

  // mit
  await transaction.mit();

} catch (err) {
  // Rollback transaction if any errors were encountered
  await transaction.rollback();
}

Whenever run, this code will output this kind of error :

transaction = await sequelize.transaction(); ^^^^^

SyntaxError: await is only valid in async function

at createScript (vm.js:80:10) at Object.runInThisContext (vm.js:139:10) at Module._pile (module.js:616:28) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12) at Function.Module._load (module.js:497:3) at Function.Module.runMain (module.js:693:10) at startup (bootstrap_node.js:191:16) at bootstrap_node.js:612:3

Project dependencies in package.json:

"body-parser": "^1.18.3" "express": "^4.16.3", "express-session": "^1.15.6", "file-system": "^2.2.2", "mysql2": "^1.5.3", "sequelize": "^4.37.10"

Node v8.11.3

Share edited Jun 18, 2018 at 13:20 Ankit Manchanda 5826 silver badges22 bronze badges asked Jun 18, 2018 at 9:55 Dariusz OchalDariusz Ochal 331 gold badge1 silver badge4 bronze badges 2
  • The error message is telling you exactly what's wrong, you can only use await inside an async function block. – Ben Fortune Commented Jun 18, 2018 at 9:59
  • 1 @BenFortune is right. You have to declare the method or function the code block of yours is in it as async or wrap the code that uses await inside a self-invoking async function using (async function(){ … your code goes here … }) edit: or wrap it in a an actual function and call it – Agash Thamo. Commented Jun 18, 2018 at 10:03
Add a ment  | 

2 Answers 2

Reset to default 4

You can only use await inside an async function, not at the top level. There's a proposal to support top-level await, but that is not currently supported in JS. Do this instead:

let transaction;
var SimpleInspectionModel = require('../models/simpleInspectionModel.js')(sequelize, { dataTypes: Sequelize.DataTypes } );

run().catch(error => console.log(error.stack));

async function run() {
  try {
    // get transaction
    transaction = await sequelize.transaction();

    // step 2
    await SimpleInspectionModel.find({}, {transaction});

    // mit
    await transaction.mit();

  } catch (err) {
    // Rollback transaction if any errors were encountered
    await transaction.rollback();
  }
}

Sequelize transactions support promises, so you should be able to use sequelize with async/await. I don't really know much about Sequelize but I wrote a blog post on using async/await with Mongoose, which is a similar tool for MongoDB, might be helpful to read.

My two cents: make sure you once again use async/await if you are working in a class method to be subsequently called. Your class method should be called with await inside an async function. You have to use async/await in each step in the call stack.

本文标签: javascriptNodejsUsing 39async39 and 39await39 with sequelize ORMStack Overflow