Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  4] [ 3]  / answers: 1 / hits: 19128  / 6 Years ago, mon, june 18, 2018, 12:00:00

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

// commit
await transaction.commit();

} 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._compile (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


More From » node.js

 Answers
26

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

// commit
await transaction.commit();

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


[#54182] Wednesday, June 13, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
debra

Total Points: 583
Total Questions: 111
Total Answers: 111

Location: Reunion
Member since Mon, Dec 28, 2020
3 Years ago
;