【问题标题】:Sails js model create is not workingSails js模型创建不起作用
【发布时间】:2017-01-12 19:25:57
【问题描述】:

当我尝试创建新的 CompanyProduct 时。我收到此错误

公司产品型号:

/**
* CompanyProduct.js
 *
 * @description :: This is a clients product inventory
 * @docs        :: http://sailsjs.org/#!documentation/models
 */

module.exports = {
    tableName: 'company_products',
    sortValues: ['companies', 'products'],

attributes: {
    'quantity': {
        'type': 'integer',
        size: 6
    },
    'cost_per_unit': {
        'type': 'integer'
    },

    // Associations
    'product': {
        'columnName': 'product_id',
        'model': 'product',
        'required': true
    },
    'company': {
        'columnName': 'company_id',
        'model': 'company',
        'required': true
    }
},

getSortValues: function() {
    return this.sortValues;
}
};  

【问题讨论】:

  • { "error": "E_VALIDATION", "status": 400, "summary": "2 个属性无效", "model": "CompanyProduct", "invalidAttributes": { "quantity" : [ { "rule": "integer", "message": "undefined 应该是一个整数(而不是 \"10\",它是一个数字)" } ], "cost_per_unit": [ { "rule" : "integer", "message": "undefined 应该是一个整数(而不是 \"100\",它是一个数字)" } ] } }
  • 您尝试从哪里创建模型? CompanyProduct.create() 或 http 请求还是什么?我们需要查看您尝试创建模型实体的代码!

标签: node.js sails.js waterline


【解决方案1】:

您似乎正在尝试将quantity 设置为"10"cost_per_unit 设置为"100" 而不是10100

可能,sails 不会将这些值解析为整数。

尝试将这种对象分配给 CompanyProduct:

{
    "quantity": 10,
    "cost_per_unit": 100
}

【讨论】:

  • CompanyProduct.create({ 数量 :10, cost_per_unit:100, product:1, company:1 }).then((companyProduct)=>{ if(!companyProduct) return res.negotiate(err ); return res.ok(companyProduct); }).catch(err=>{ return res.negotiate(err); })
  • 这些字段在你的模型中有什么类型?
  • 'quantity': { 'type': 'integer', size: 6 }, cost_per_unit: {` type: 'integer' }
  • 您是否使用 MySQL 作为数据库?
猜你喜欢
  • 2018-09-07
  • 1970-01-01
  • 1970-01-01
  • 2016-12-22
  • 2015-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多