创建记录时Sails.js验证错误:应该是一个数字(而不是"xx",这是一个字符串)

mwo*_*ton 2 mysql sails.js waterline

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

// 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)
Run Code Online (Sandbox Code Playgroud)

从调试日志记录中可以看出,item.content确实是一个数字,但由于某种原因,它在某处转换为字符串,可能是适配器.我怎么能绕过这个?

mwo*_*ton 13

通过更改模型属性来content: 'integer'解决问题.integer使用numeric验证器接受一个数字或一个只包含一个数字的字符串.