【问题标题】:sailsjs 1.0 model attribute type date columntype datetime error out in sailsjsSailsjs 1.0 模型属性类型日期列类型日期时间错误在sailsjs 中
【发布时间】:2018-03-20 08:05:20
【问题描述】:

在sailsjs 1.0@beta 中,我使用mysql-sails@beta 适配器将模型链接到mysql 表中的视图。

模型配置如下:

attributes: {
    id: {type:'number',  required: true},
    'release_date': { type: 'string', columnType: 'datetime' }
  }

尽管当我查询模型时,sails 会在每个日期列显示一堵错误墙:

Warning: After transforming columnNames back to attribute names, a record
in the result has a value with an unexpected data type for property `release_date`.
The corresponding attribute declares `type: 'string'` but instead
of that, the actual value is:
```
2016-04-07T05:00:00.000Z
```

我已尝试将类型设置为“日期时间”,尽管不再受支持。

The type "datetime" is no longer supported.  To use this type in your model, change
`type` to one of the supported types and set the `columnType` property to a column 
type supported by the model's adapter, e.g. { type: 'string', columnType: 'datetime' }

我找不到任何关于什么是告诉sailsjs 模型它的日期的正确方法的文档,以便sailsjs 1.0b 不会出错,并且有没有办法告诉sails 它是一个只读模型视图?

【问题讨论】:

    标签: mysql datetime model sails.js


    【解决方案1】:

    经过更多挖掘,在https://github.com/balderdashy/waterline/issues/1497上找到了一张关于此的票

    使用 ref 代替字符串的日期时间问题的修复:

    attributes: {
        id: {type:'number',  required: true},
        'release_date': { type: 'ref', columnType: 'datetime' }
      }
    

    【讨论】:

    • 我有类似的问题,但使用的是 MongoDB 数据库。我尝试了您的解决方案,但没有奏效。你会碰巧知道我该如何解决这个问题吗?
    • 在这种情况下,这是否意味着 ref 就像一个 javascript 对象?如果没有其他合适的,基本上使用 ref?
    • @Nag 你找到解决方案了吗?
    【解决方案2】:

    一种解决方法可能是当前在用户模型中的 SailsJS 默认 Web 应用程序中找到的方法:

    lastSeenAt: {
          type: 'number',
          description: 'A JS timestamp (epoch ms) representing the moment at which this user most recently interacted with the backend while logged in (or 0 if they have not interacted with the backend at all yet).',
          example: 1502844074211
        },
    

    然后在 UI 中,您可以通过执行以下操作轻松地将其转换为人类可读的格式:

    new Date(user.lastSeenAt)
    

    【讨论】:

    • 是的,Sails v1 应用程序还有一个 JSTimestamp 组件,因此您可以轻松地将其转换为前端。
    猜你喜欢
    • 1970-01-01
    • 2022-01-20
    • 2010-10-05
    • 2016-12-14
    • 1970-01-01
    • 2022-11-17
    • 2018-06-06
    • 1970-01-01
    • 2020-10-07
    相关资源
    最近更新 更多