私はnodejsを学ぶためにnodejs + sequelize + jade web-appに取り組んでいます。基本的なことはすべて非常に明確ですが、今はアンティを上げたいと思っています。私はブランドと呼ばれるオブジェクト(テーブル)を持っています。対象商品はブランドと一対多の関係にある。私がやりたいことは、すべてのブランドを見つけて Jade テンプレートに表示し、その下に製品をリストすることです。
ここにいくつかの基本的なコードがあります
var Brand = sequelize.import(application_root + "/models/brand.js");
var Product = sequelize.import(application_root + "/models/product.js");
Brand.hasMany(Product, { as: 'Products', foreignKey: 'brand'});
Product.belongsTo(Brand, { foreignKey: 'key'});
私が行っているブランドや製品を紹介するルートでは、
Brand.findAll().error(errorHandler).success(function(brands)
{
brands[0].getProducts().success(function(products) {
brands[0].products = products;
});
res.render('listOfbrands.jade', { title: 'List of brands', items: brands});
});
最も奇妙なことは、console.log が実行されたときにクエリが起動されていることを確認できますが、ブランドの主キーを使用して正しいクエリが作成されないことです。select * from products where "brand" is null であるクエリ
また、このようにして Jade テンプレートでアクセスするためにサブコレクションを正しく割り当てているかどうかを知りたい
ul
each item in items
li(class='brandItem')= item.name
ul
each product in item.products
li=product.name