【问题标题】:Sails.js validation error on creating record: should be a number (instead of "xx", which is a string)Sails.js 在创建记录时验证错误:应该是一个数字(而不是“xx”,它是一个字符串)
【发布时间】:2015-02-17 00:53:12
【问题描述】:

我正在尝试在表中创建一条记录,它与其他现有模型共享一些属性。

// model:
module.exports = {
  attributes: {
    ...
    contentType: 'string', // This is a combination foreign-key,
                           // which is not currently
    content: 'number',     // supported in waterline
    ...
  }
}

// creating code:
sails.log('item.content:', typeof item.content, item.content);
UserContentLibrary.create({
  user: prodSubscription.user,
  productContentSubscription: pcs.id,
  contentType: item.contentType,
  content: item.content,
}, function uclCreateCB(err, ucl){
  if (err){
    sails.log.error(err);
  }
  itemDone();
});

// error (console output):
debug: item.content: number 3
error: Error (E_VALIDATION) :: 1 attribute is invalid
... 
Invalid attributes sent to UserContentLibrary:
  content
    `undefined` should be a number (instead of "30", which is a string)

从调试日志中可以看出,item.content 确实是一个数字,但是由于某种原因,它在某个地方(可能是适配器)被转换为字符串。我该如何解决这个问题?

【问题讨论】:

  • 如果你先做parseInt(content)呢?
  • 你有任何回调在运行吗?因为它不仅仅是将其设为字符串,而是将其设为 30 而不是 3。您可能需要运行 node-debugger 并查找数字更改的位置。

标签: mysql sails.js waterline


【解决方案1】:

通过将模型属性更改为content: 'integer',问题得到解决。 integer 使用 numeric 验证器,它接受数字或仅包含数字的字符串。

【讨论】:

    猜你喜欢
    • 2019-08-27
    • 2018-08-12
    • 2017-02-14
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 2018-04-16
    相关资源
    最近更新 更多