I have a scenario involving two models, Contact and Message, with a join model named ContactMessage.
Contact.belongsToMany(models.Message, {
through: 'ContactMessage'
})
Message.belongsToMany(models.Contact, {
through: 'ContactMessage'
})
My objective is to send a message from a sender to a receiver, while also adding the message ID and the contacts ID to the joinTable. However, I am facing an issue where this data is not being added as intended.
Below is the code snippet:
sendSms(req, res) {
return Contact
.find( {
where: {
contact_phone: req.body.sender
}
}
)
.then(sender => {
if (sender) {
let users = [sender]
return Contact
.find({
where: {
contact_phone: req.body.reciever
}
}).then(reciever => {
users.push(reciever)
return users
})
} else {
console.log(`${sender} does not exist`)
}
}).then (users => {
if (!users) {
return res.status(404).send({
message: 'Users Not Found',
});
}
return Message.create({
sms
} = req.body)
.then(message => {
message.setContacts([users], {status: 'sent'})
return res.status(200).send(message)
})
.catch((error) => res.status(400).send(error));
})
.catch((error) => res.status(400).send(error));
}
I'm curious about what could be going wrong here. Every time I attempt to run the api, I encounter the following error:
Unhandled rejection SequelizeDatabaseError: operator does not exist: character varying = integer
at Query.formatError (/Users/andeladeveloper/Documents/projects/sms-app/node_modules/sequelize/lib/dialects/postgres/query.js:363:16)
at query.catch.err (/Users/andeladeveloper/Documents/projects/sms-app/node_modules/sequelize/lib/dialects/postgres/query.js:86:18)
at tryCatcher (/Users/andeladeveloper/Documents/projects/sms-app/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/Users/andeladeveloper/Documents/projects/sms-app/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/Users/andeladeveloper/Documents/projects/sms-app/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/Users/andeladeveloper/Documents/projects/sms-app/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/Users/andeladeveloper/Documents/projects/sms-app/node_modules/bluebird/js/release/promise.js:690:18)
at _drainQueueStep (/Users/andeladeveloper/Documents/projects/sms-app/node_modules/bluebird/js/release/async.js:138:12)
at _drainQueue (/Users/andeladeveloper/Documents/projects/sms-app/node_modules/bluebird/js/release/async.js:131:9)
at Async._drainQueues (/Users/andeladeveloper/Documents/projects/sms-app/node_modules/bluebird/js/release/async.js:147:5)
at Immediate.Async.drainQueues (/Users/andeladeveloper/Documents/projects/sms-app/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:789:20)
at tryOnImmediate (timers.js:751:5)
at processImmediate [as _immediateCallback] (timers.js:722:5)