【问题标题】:Destructuring a Sequelize query解构 Sequelize 查询
【发布时间】:2020-08-10 06:01:05
【问题描述】:

我无法解构一个简单的 Sequelize 查询。

const character = await Character.findAll({
  order: ['created_at'],
  attributes: ['id', 'name', 'created_at'],
});

我想访问列表的“created_at”,但我尝试

const { created_at } = character;

但它返回未定义。

我需要从 created_at 日期中减去当前日期。

谁能告诉我我做错了什么或者我该怎么做? 如果需要,我已经安装了 Moment.js。

查询返回

{ "id": 10, "name": "John Doe", "created_at": "2020-08-08T20:52:06.262Z" }, 
{ "id": 11, "name": "Mary Jane", "created_at": "2020-08-08T21:01:49.562Z" }, 
{ "id": 12, "name": "Clark Kent", "created_at": "2020-08-08T21:02:51.948Z" } 
]

【问题讨论】:

  • [ { "id": 10, "name": "John Doe", "created_at": "2020-08-08T20:52:06.262Z" }, { "id": 11, “名称”:“玛丽珍”,“created_at”:“2020-08-08T21:01:49.562Z”},{“id”:12,“名称”:“克拉克肯特”,“created_at”:“2020- 08-08T21:02:51.948Z" } }

标签: node.js sequelize.js


【解决方案1】:

在您的情况下,您正在尝试从数组中进行解构。但它只适用于对象。

要让它工作,你应该使用索引。

类似:

const { created_at } = character[0];

请注意,它只会破坏字符数组中包含的结果中的位置zero。如果您不希望位置为零,您应该使用另一个函数来选择所需的结果。

【讨论】:

    猜你喜欢
    • 2017-03-23
    • 2017-05-10
    • 1970-01-01
    • 2021-06-11
    • 2017-03-19
    • 2018-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多