【问题标题】:Error on emit a Sequelize result with Socket.io [Express, Socket.io, Sequelize]使用 Socket.io [Express, Socket.io, Sequelize] 发出 Sequelize 结果时出错
【发布时间】:2019-12-21 23:36:38
【问题描述】:

我在我的 Express API 上有一个简单的 put 请求,在我使用 Sequelize ORM 进行更新后,我无法使用 Socket.io 发出。

我只能发出其他对象,例如,我可以发出这个 {"hello": "world"},但我不能发出更新的结果。

这是代码块:

module.exports = {
  async update(req, res) {
    const { body } = req;
    const { task_id } = req.params;

    const task = await ProjectTask.findByPk(task_id);

    task.update({
      name: body.title,
      description: body.description,
    });


    req.io.emit('update', task); // Here it is the socket.io that brakes the code
    console.log(task); // this works 


    return res.json(task); // also this
  },
}

错误: (节点:8923)UnhandledPromiseRejectionWarning:RangeError:超出最大调用堆栈大小 在 _removeBlobs (/Users/guifeliper/nodejs/node_modules/socket.io-parser/binary.js:99:24) 在 _removeBlobs (/Users/guifeliper/nodejs/node_modules/socket.io-parser/binary.js:126:9) 在 _removeBlobs (/Users/guifeliper/nodejs/node_modules/socket.io-parser/binary.js:130:9) 在 _removeBlobs (/Users/guifeliper/nodejs/node_modules/socket.io-parser/binary.js:130:9) 在 _removeBlobs (/Users/guifeliper/nodejs/node_modules/socket.io-parser/binary.js:130:9) 在 _removeBlobs (/Users/guifeliper/nodejs/node_modules/socket.io-parser/binary.js:130:9) 在 _removeBlobs (/Users/guifeliper/nodejs/node_modules/socket.io-parser/binary.js:130:9) 在 _removeBlobs (/Users/guifeliper/nodejs/node_modules/socket.io-parser/binary.js:130:9) 在 _removeBlobs (/Users/guifeliper/nodejs/node_modules/socket.io-parser/binary.js:130:9) 在 _removeBlobs (/Users/guifeliper/nodejs/node_modules/socket.io-parser/binary.js:130:9) 在 _removeBlobs (/Users/guifeliper/nodejs/node_modules/socket.io-parser/binary.js:130:9) 在 _removeBlobs (/Users/guifeliper/nodejs/node_modules/socket.io-parser/binary.js:130:9) 在 _removeBlobs (/Users/guifeliper/nodejs/node_modules/socket.io-parser/binary.js:130:9) 在 _removeBlobs (/Users/guifeliper/nodejs/node_modules/socket.io-parser/binary.js:130:9) 在 _removeBlobs (/Users/guifeliper/nodejs/node_modules/socket.io-parser/binary.js:130:9) 在 _removeBlobs (/Users/guifeliper/nodejs/node_modules/socket.io-parser/binary.js:130:9) 在 _removeBlobs (/Users/guifeliper/nodejs/node_modules/socket.io-parser/binary.js:130:9) 在 _removeBlobs (/Users/guifeliper/nodejs/node_modules/socket.io-parser/binary.js:130:9) 在 _removeBlobs (/Users/guifeliper/nodejs/node_modules/socket.io-parser/binary.js:130:9) 在 _removeBlobs (/Users/guifeliper/nodejs/node_modules/socket.io-parser/binary.js:130:9) 在 _removeBlobs (/Users/guifeliper/nodejs/node_modules/socket.io-parser/binary.js:130:9) 在 _removeBlobs (/Users/guifeliper/nodejs/node_modules/socket.io-parser/binary.js:130:9) (节点:8923)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。此错误源于在没有 catch 块的情况下抛出异步函数内部,或拒绝未使用 .catch() 处理的承诺。 (拒绝编号:1) (节点:8923)[DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。将来,未处理的 Promise 拒绝将使用非零退出代码终止 Node.js 进程。

编辑:可能是因为项目格式? 这就是我们在任务变量上的内容:

{
  "id": 10971,
  "name": "Change the background image 5",
  "description": "save description 2",
  "dueEndDate": null,
  "status": 3,
  "deleted": {
    "type": "Buffer",
    "data": [
      0
    ]
  },
  "closed": {
    "type": "Buffer",
    "data": [
      0
    ]
  },
  "createdAt": "2019-12-18T16:06:57.000Z",
  "modifiedAt": "2019-12-21T14:43:39.000Z",
  "sprint_id": 41
}

未解析:

ProjectTask {
  dataValues:
   { id: 10971,
     name: 'Change the background image 5',
     description: 'save description 2',
     dueEndDate: null,
     status: 3,
     deleted: <Buffer 00>,
     closed: <Buffer 00>,
     createdAt: 2019-12-18T16:06:57.000Z,
     modifiedAt: 2019-12-21T14:43:39.000Z,
     sprint_id: 41 },
  _previousDataValues:
   { id: 10971,
     name: 'Change the background image 5',
     description: 'save description 2',
     dueEndDate: null,
     status: 3,
     deleted: <Buffer 00>,
     closed: <Buffer 00>,
     createdAt: 2019-12-18T16:06:57.000Z,
     modifiedAt: 2019-12-21T14:43:39.000Z,
     sprint_id: 41 },
  _changed: {},
  _modelOptions:
   { timestamps: true,
     validate: {},
     freezeTableName: true,
     underscored: false,
     paranoid: false,
     rejectOnEmpty: false,
     whereCollection: { id: '10971' },
     schema: null,
     schemaDelimiter: '',
     defaultScope: {},
     scopes: {},
     indexes: [],
     name: { plural: 'ProjectTasks', singular: 'ProjectTask' },
     omitNull: false,
     createdAt: 'createdAt',
     updatedAt: 'modifiedAt',
     sequelize:
      Sequelize {
        options: [Object],
        config: [Object],
        dialect: [MysqlDialect],
        queryInterface: [QueryInterface],
        models: [Object],
        modelManager: [ModelManager],
        connectionManager: [ConnectionManager],
        importCache: {} },
     hooks: {} },
  _options:
   { isNewRecord: false,
     _schema: null,
     _schemaDelimiter: '',
     raw: true,
     attributes:
      [ 'id',
        'name',
        'description',
        'dueEndDate',
        'status',
        'deleted',
        'closed',
        'createdAt',
        'modifiedAt',
        'sprint_id' ] },
  isNewRecord: false }

【问题讨论】:

  • 显示task的内容。
  • 也添加了未解析的版本,谢谢 Marcos。
  • 这就是问题所在。检查我的答案。

标签: node.js express socket.io sequelize.js


【解决方案1】:

我从来没有用过sequelize,但是task里面肯定有一些循环引用。

因此,您可以在sequelize 查询中使用raw: true 选项,也可以使用.toJSON、解构或类似方法发送您想要的属性。

您提到您有对象的“已解析”版本,发送它而不是 sequelize 对象。

除了使用raw,以下应该可以工作:

req.io.emit('update', task.toJSON()); // will only have table attributes
req.io.emit('update', JSON.stringify(task)); // you'll get it as string in FE

【讨论】:

  • 我试过raw: true,但很遗憾没有用。我的意思是 res.json 只返回原始数据 (json)。
  • @GuilhermeFelipeReis 检查更新的答案,使用 .toJSON 或其他替代方法之一。
  • 太棒了,刚刚工作了 .toJSON() 和 JSON.stringify(task)。你知道这里发生了什么吗?它只是一个循环引用吗?因为我每次都在重用任务?
  • 不,这不是因为引用自身的 sequelize 属性。无论如何,您只想发送表格数据。所以这就是你使用.toJSON的原因
猜你喜欢
  • 1970-01-01
  • 2017-08-23
  • 2018-03-07
  • 2016-09-30
  • 1970-01-01
  • 2014-07-07
  • 2015-12-22
  • 2020-10-22
  • 2016-09-20
相关资源
最近更新 更多