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

150
Views
¿Cómo actualizo un modelo Sequelize con una asociación?

Estoy tratando de actualizar un modelo de secuela (llamado conversación) cada vez que inserto un valor en su asociación (llamado mensaje). El modelo de conversación no se actualiza automáticamente y no he encontrado ninguna información útil en los documentos.

 const router = require("express").Router(); const { Conversation, Message } = require("../../db/models"); const onlineUsers = require("../../onlineUsers"); // expects {recipientId, text, conversationId } in body (conversationId will be null if no conversation exists yet) router.post("/", async (req, res, next) => { try { if (!req.user) { return res.sendStatus(401); } const senderId = req.user.id; const { recipientId, text, conversationId, sender } = req.body; // if we already know conversation id, we can save time and just add it to message and return if (conversationId) { const message = await Message.create({ senderId, text, conversationId }); Conversation.update({ }) return res.json({ message, sender }); } // if we don't have conversation id, find a conversation to make sure it doesn't already exist let conversation = await Conversation.findConversation( senderId, recipientId ); if (!conversation) { // create conversation conversation = await Conversation.create({ user1Id: senderId, user2Id: recipientId, }); if (onlineUsers.includes(sender.id)) { sender.online = true; } } const message = await Message.create({ senderId, text, conversationId: conversation.id, }); res.json({ message, sender }); } catch (error) { next(error); } }); module.exports = router;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

para la actualización, debe cambiar o agregar datos en su registro y el segundo argumento es su condición para filtrar usuarios (si no aprobó el argumento para la condición, significa que no deseaba filtrar conversaciones y luego actualizar todos los registros)

 await conversation.update( { title: 'sample title' }, { where: { : 2 } } )
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!