Organize Associations - Use a Promise Instead

I am trying to combine 3 tables together Products , Suppliers and Categories , and then get a row with SupplierID = 13 . I read How to implement many, many associations in sequelize , explains how to bind 0:M

DB Model: enter image description here

Code:

 var Sequelize = require('sequelize') var sequelize = new Sequelize('northwind', 'nodejs', 'nodejs', {dialect: 'mysql',}) var Project = require('sequelize-import')(__dirname + '/models', sequelize, { exclude: ['index.js'] }); Project.Suppliers.hasMany(Project.Products, {foreignKey: 'SupplierID'}); Project.Products.belongsTo(Project.Suppliers, {foreignKey: 'SupplierID'}); Project.Categories.hasMany(Project.Products, {foreignKey: 'CategoryID'}); Project.Products.belongsTo(Project.Categories, {foreignKey: 'CategoryID'}); Project.Products .find({ where: { SupplierID: 13 }, include: [ Project.Suppliers, Project.Category, ] }) .success(function(qr){ if (qr == null) throw "Err"; console.log("---"); console.log(qr); }) .error(function(err){ console.log("Err"); }); 

Journal

  EventEmitter#success|ok is deprecated, please use promise-style instead. EventEmitter#failure|fail|error is deprecated, please use promise-style instead. Err 
+6
source share
2 answers

Update: January 15, 15 - the .finally() handler is added. It also indicates how .then() is served with the argument of the previous handler and how to execute the next consecutive request.

The .success , .error and .done are deprecated. Errors are not critical, and they are likely to support backward compatibility. But you still have to change it.

As promised by the Specification: http://wiki.commonjs.org/wiki/Promises/A

Now you should do the following style:

 db.Model.find(something) .then(function(results) { //do something with results //you can also take the results to make another query and return the promise. return db.anotherModel.find(results[0].anotherModelId); }).then(function(results) { //do something else }).catch(function(err) { console.log(err); }).finally(function() { // finally gets called always regardless of // whether the promises resolved with or without errors. // however this handler does receive any arguments. }); 

In short:

Use .then instead of .success

Use .catch instead of .error

Use .finally instead of .done * note: .finally will always be called independently.

+33
source

I had the same problem with you, you can change .success and .error with one .done(function(err, result)) to do both operations, and the warning will also disappear.

+1
source

Source: https://habr.com/ru/post/977917/


All Articles