Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 向联接表添加数据_Javascript_Sql_Postgresql_Express_Sequelize.js - Fatal编程技术网

Javascript 向联接表添加数据

Javascript 向联接表添加数据,javascript,sql,postgresql,express,sequelize.js,Javascript,Sql,Postgresql,Express,Sequelize.js,我有两个模型,Contact和Message,其中有一个ContactMessage的连接模型 Contact.belongsToMany(models.Message, { through: 'ContactMessage' }) Message.belongsToMany(models.Contact, { through: 'ContactMessage' }) 我正在从发送者向接收者发送消息,我想将消息Id和联系人Id添加到joinTable中,但它没有添加 这就是我所拥有的

我有两个模型,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)

我在迁移文件中犯了一个错误,
ContactMessage
我使用字符串作为数据类型,而不是整数

现在看起来是这样的:

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
      }
    });

您应该告诉我们您正在使用哪些库(Sequelize是您的情况),而不必阅读错误日志。您能告诉我们这两种型号的主键类型吗?联系人和消息?我最终找到了答案。在迁移过程中,我将数据类型设置为
string
,而不是
integer
。我现在好了。