Sequelize.js 从关联表获取信息

Sequelize.js 从关联表获取信息,sequelize.js,Sequelize.js,我有两张桌子: ---id---userid--- and ---id---productid---productname--- 因此,它由以下代码关联: Product.belongsToMany(User, {through: 'resultingproducts', foreignKey: 'userid'}); User.belongsToMany(Product, {through: 'resultingproducts', foreignKey: 'productid'}); 我

我有两张桌子:

---id---userid--- and ---id---productid---productname---
因此,它由以下代码关联:

Product.belongsToMany(User, {through: 'resultingproducts', foreignKey: 'userid'});
User.belongsToMany(Product, {through: 'resultingproducts', foreignKey: 'productid'});
我已尝试为当前用户从产品中获取产品

        Product.sync()
        .then(() => {
            Product.findAll({
                include: [User],
                through: {
                    where: {
                        userid: userId
                }
        })
但这段代码为数据库中的所有用户返回产品。
有人能帮我吗?

你真的应该查询用户模型而不是产品模型。 尝试将代码更改为“以下”以查找与“此”用户“相关”的所有产品

User.findAll({
  include: {
    model: Product, // Product is Product model
    through: {attributes: []} // this will remove rows from join table in the result
  },
  where: { userId: userId } // note, where clause is outside 'include' and 'through'
})