Updating a many to many join table using sequelize for nodejs

I have a Products table and a Categories table. A single Product can have many Categories and a single Category can have many Products, therefore I have a ProductsCategories table to handle the many-to-many join.

In the example below, I'm trying to associate one of my products (that has an ID of 1) with 3 different categories (that have IDs of 1, 2, & 3). I know something is off in my code snippet below because I'm getting an ugly SQL error message indicating that I'm trying to insert an object into the ProductsCategories join table. I have no idea how to fix the snippet below or if I'm even on the right track here. The Sequelize documentation is pretty sparse for this kind of thing.

models.Product.find({ where: {id: 1} }).on('success', function(product) {
  models.Category.findAll({where: {id: [1,2,3]}}).on('success', function(category){
    product.setCategories([category]);
  });      
});

I'd really appreciate some help here, thanks. Also, I'm using Postgres, not sure if that matters.

models.Category.findAll returns an array. By doing setCategories([category]); you are wrapping that array in an array. Try changing it to setCategories(category); instead

I think you are close. I had a similar issue with some of my code. Try iterating over your found categories and then add them. I think this might do the trick.

models.Category.findAll({where: {id: [1,2,3]}}).on('success', function(category){
            for(var i=0; i<category.length; i++){
                product.setCategories([category[i]]);
            }
      });