Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
Sequelize - .update - setTags is not a function

"setTags is not a function"

I have Many-to-Many relationships for Posts and Tags.

db.tag.belongsToMany(db.post, {
  through: 'post_tags',
  foreignKey: 'tagId',
  otherKey: 'postId',
})
db.post.belongsToMany(db.tag, {
  through: 'post_tags',
  foreignKey: 'postId',
  otherKey: 'tagId',
})

I'm trying to update the Post's content, including the associated Tags.


This beginning at line 7:

  • Check for tags
  • Match found tags
  • Set the tags of post

exports.update = (req, res) => {
      const id = req.params.id
      Post.update(req.body, {
        where: { id: id },
      })
        .then((number) => {
          if (req.body.tags) {
            Tag.findAll({
              where: {
                name: {
                  [Op.or]: req.body.tags,
                },
              },
            }).then((tag_items) => {
              res.setTags(tag_items)
            })
          }
          if (number == 1) {
            res.send({
              message: 'This post attempt was successful.',
            })
          } else {
            res.send({
              message: `Problem with updating id=${id}. May not exist, or req.body could be empty!`,
            })
          }
        })
        .catch((err) => {
          res.status(500).send({
            message: 'There was an error updating post id=' + id,
          })
        })
    }

I use something very similar for creating a Post.

I was hoping this would work like how it does for that.

I have read so much of the documents and online searches.

At this point I feel like it must be something simple I'm overlooking.

Maybe even a misspelling, or bad positioning of something.


I have tried creating response data with findByPk as the Post.

Then running this with setTags, I still get the same error.

Not sure if maybe because Post.update returns different data?

Than doing Post.create, I thought I read something on this.

But I had tried adding another parameter and wasn't getting expected results.


If you can offer any advice, it would be greatly appreciated.

Thank you!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You called setTags in res which is not a Sequeize model of Post. You need to get Post instance first like this:

 Post.findOne({
        where: { id: id },
      }).then((post) => {
if (req.body.tags) {
            Tag.findAll({
              where: {
                name: {
                  [Op.or]: req.body.tags,
                },
              },
            }).then((tag_items) => {
              post.setTags(tag_items)
            })
          }  
})

And I recommend to use async/await to have straint-forward code instead of chain of then in this case:

exports.update = async (req, res) => {
      const id = req.params.id
   try {
      const number = await Post.update(req.body, {
        where: { id: id },
      })
      if (req.body.tags) {
        const post = await Post.findOne({
          where: { id: id },
        })
        const tag_items = await Tag.findAll({
           where: {
             name: {
               [Op.or]: req.body.tags,
             },
          });
          post.setTags(tag_items)
      }
      if (number == 1) {
         res.send({
           message: 'This post attempt was successful.',
         })
      } else {
         res.send({
           message: `Problem with updating id=${id}. May not exist, or req.body could be empty!`,
         })
      }
  } catch((err) {
       res.status(500).send({
         message: 'There was an error updating post id=' + id,
       })
  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!