我有两个模型,Contact和Message,它们具有ContactMessage的联接模型。
Contact.belongsToMany(models.Message, {
through: 'ContactMessage'
})
Message.belongsToMany(models.Contact, {
through: 'ContactMessage'
})
我正在从发送者向接收者发送一条消息,我想将消息ID和联系人ID添加到joinTable中,但没有添加。
这就是我所拥有的:
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));
}
请问我在做什么错? 每当我尝试运行api时,我都会收到以下错误消息
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)
答案 0 :(得分:1)
在迁移文件中,我为ContactMessage
输入了一个错误,因为我使用String代替了INTEGER作为数据类型。
现在看起来像这样:
return queryInterface.createTable('ContactMessages', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
contact_id: {
type: Sequelize.INTEGER,
references: {
model: 'Contacts',
key: 'id'
}
},
message_id: {
type: Sequelize.INTEGER,
references: {
model: 'Messages',
key: 'id'
}
},
status: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});