续订关联 – 请使用承诺风格
我试图join3个表在一起的Products
, Suppliers
和Categories
,然后获得行与SupplierID = 13
。 我已经阅读了如何实现多对多的关联 ,并解释如何关联0:M
。
DB型号:
码:
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"); });
日志:
EventEmitter#success|ok is deprecated, please use promise-style instead. EventEmitter#failure|fail|error is deprecated, please use promise-style instead. Err
更新:15年1月15日 – 添加.finally()
处理程序。 还指出了如何使用前面的处理程序的参数来提供.then()
,以及如何执行下一个sorting的查询。
.success
, .error
和.done
处理程序已弃用。 这些错误并不重要,它们可能会向后兼容。 但是你应该改变它。
按照承诺A规范: http : //wiki.commonjs.org/wiki/Promises/A
你现在应该做以下的风格:
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. });
简而言之:
使用.success
而不是.success
使用.catch
而不是.error
使用.finally
而不是.done
*注意: .finally
总是会被调用。
我有同样的问题,你可以改变.success
和.error
与单一的.done(function(err, result))
做两个操作,警告消息也消失了。