6

I'm trying to join 3 tables together Products, Suppliers and Categories and then get row with SupplierID = 13. I have read How to implement many to many association in sequelize, there is explained how to associate 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");
    });

Log:

    EventEmitter#success|ok is deprecated, please use promise-style instead.
    EventEmitter#failure|fail|error is deprecated, please use promise-style instead.
    Err
Community
  • 1
  • 1
Matjaž
  • 2,096
  • 3
  • 35
  • 55

2 Answers2

33

Update: 15 Jan 15 - added .finally() handler. Also indicated how .then() is being fed with an argument from the previous handler and how to perform the next sequenced query.

The .success, .error and .done handlers are deprecated. The errors are not critical and it is likely that backwards compatibility will be maintained on them. But you should still change it.

As per promise A specs: http://wiki.commonjs.org/wiki/Promises/A

You now 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 finally handler does not receive any arguments.
  });

In short:

Use .then instead of .success

Use .catch instead of .error

Use .finally instead of .done *note: .finally will always get called regardless.

Sadern Alwis
  • 104
  • 1
  • 4
  • 17
Calvintwr
  • 8,308
  • 5
  • 28
  • 42
  • Actually, `finally` would be a better replacement for `done`, since it is called regardless of the success or not. But it does not have access to neither err nor the result – Jan Aagaard Meier Nov 11 '14 at 08:04
  • That looks cool, but I am using Eclipse(nodeclipse) and it displays me compilation error when using .catch (The error says: "Syntax error on token "catch", Identifier expected"). How to avoid this error? – Milos Gavrilov May 12 '15 at 00:22
  • most probably some code error like missing quotations or brackets. – Calvintwr May 13 '15 at 15:47
  • Actually syntax is not problem, for example, this code causes error: doSomethingAsync().then().catch() – Milos Gavrilov Jun 25 '15 at 22:10
1

i had the same issue with you, you can change .success and .error with single .done(function(err, result)) to do both operation and the warning message disappear too.

dandice
  • 179
  • 3
  • 13
  • [`done` is also deprecated](https://github.com/sequelize/sequelize/wiki/Upgrading-to-2.0) – Domi Oct 02 '15 at 04:44
  • Also this is a classic promise anti-pattern: https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns#the-thensuccess-fail-anti-pattern – sethreidnz Nov 14 '15 at 01:29