续集关联 - 请改用 promise 样式

2022-01-19 00:00:00 node.js orm javascript sequelize.js

我正在尝试将 3 个表连接在一起 ProductsSuppliersCategories,然后使用 SupplierID = 13.我已阅读

您现在应该执行以下样式:

db.Model.find(something).then(函数(结果){//对结果做一些事情//您也可以将结果进行另一个查询并返回承诺.返回 db.anotherModel.find(results[0].anotherModelId);}).then(函数(结果){//做其他事情}).catch(函数(错误){控制台日志(错误);}).finally(函数() {//finally 总是被调用,不管//承诺是否解决了错误.//然而这个 finally 处理程序不接收任何参数.});

简而言之:

使用 .then 代替 .success

使用 .catch 而不是 .error

使用 .finally 而不是 .done*注意:.finally 无论如何都会被调用.

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:

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

解决方案

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.

相关文章